How to get `python` to run Python 3 in WSL bash?
Asked Answered
G

3

17

When I type python into my bash shell (Windows Subsystem for Linux) in Windows 10 Home, I get the following error message:

The program 'python' can be found in the following packages:
 * python-minimal
 * python3
Try: sudo apt install <selected package>

I've tried installing python3 but am told it's already installed and up to date.

I've tried uninstalling python-minimal but am told it's not installed (!)

Why am I seeing two "competing" packages for Python? How can I fix the conflict and configure my WSL bash to run Python 3 from python?

Gerda answered 30/1, 2018 at 14:48 Comment(11)
You could do python3, otherwise, do type -a python and it will give you an idea of what you have change.Brill
Thanks. I need it to run from python. type -a python gives me: -bash: type: python: not foundGerda
create an alias: alias python="python3". If you're managing many versions of python, you might want to look into pyenvDoti
python in linux world as a CLI command almost always means python2 and not python3. Make sure that you have python2 installed (sudo apt install python). DO NOT alias python to python3 - this is some bad advice! To run python3, you have to specify python3 on the CLI.Mavilia
@srdjan-grubor thank you. Installing python (v2.7) did the trick and I now understand that for v3.6 I need to stick with python3Gerda
@SrdjanGrubor I don't see why an alias would be bad (assuming you don't want to manually run python2 at some point). It's not a symlink.Doti
@PatrickHaugh Aliases are fine when they don't map to a legitimate command but if he had this python->python3 alias and then ran some script that had python <do_someting> in it, the shell would try to invoke python3 when the writer of that script was explicitly intending to run <do_something> on python2 so it would likely break. That plus the fact that most Linux bin commands are actually python scripts, this would cause all kinds of funky problems.Mavilia
A system script should use a shebang such as "#!/usr/bin/python" or "#!/usr/bin/python3". Even "#!/usr/bin/env python" wouldn't be an issue with aliases. Also, aliases aren't available in shell scripts, at least not normally. I see no practical problem with assigning a "python" alias for the interactive shell, though the OP should really get in the habit of developing with virtual environments, in which case this isn't an issue.Monatomic
@srdjan-grubor would you like to post your solution as an answer so I can accept it?Gerda
@Gerda Copy pasted it :)Mavilia
Python 2 is dead. python should run Python 3.x. If you have a script that relies on Python 2, use the full path to your binary. $PATH is to help humans, not scripts.Disease
M
7

python in linux world as a CLI command almost always means python2 and not python3. Make sure that you have python2 installed (sudo apt install python).

DO NOT alias python to python3 - this is some bad advice!

To run python3, you have to specify python3 on the CLI.

Mavilia answered 17/2, 2018 at 1:23 Comment(3)
Not an answer and not a good advice or bad, it is just a preference of developer, this should not be an answer at all.Roldan
No. python2 is dead. python should run Python 3.x.Disease
This was perfectly good advice in 2018, though.Ropedancer
D
16

If you're running Ubuntu 20 under WSL, there is a new package called python-is-python3:

cameron@Nook:/mnt/c/Users/camer$ sudo apt install python-is-python3
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  python-is-python3
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 2364 B of archives.
After this operation, 10.2 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 python-is-python3 all 3.8.2-4 [2364 B]
Fetched 2364 B in 0s (7208 B/s)
Selecting previously unselected package python-is-python3.
(Reading database ... 33571 files and directories currently installed.)
Preparing to unpack .../python-is-python3_3.8.2-4_all.deb ...
Unpacking python-is-python3 (3.8.2-4) ...
Setting up python-is-python3 (3.8.2-4) ...
cameron@Nook:/mnt/c/Users/camer$ python --version
Python 3.8.10

Alternatively, you could use update-alternatives:

sudo update-alternatives --install /usr/bin/python python $(readlink -f $(which python3)) 3
Disease answered 21/7, 2021 at 22:5 Comment(0)
M
7

python in linux world as a CLI command almost always means python2 and not python3. Make sure that you have python2 installed (sudo apt install python).

DO NOT alias python to python3 - this is some bad advice!

To run python3, you have to specify python3 on the CLI.

Mavilia answered 17/2, 2018 at 1:23 Comment(3)
Not an answer and not a good advice or bad, it is just a preference of developer, this should not be an answer at all.Roldan
No. python2 is dead. python should run Python 3.x.Disease
This was perfectly good advice in 2018, though.Ropedancer
S
-1

If your script may works with python3, alias is the simple way:

sudo ln -s /usr/bin/python3 /usr/bin/python
Selfcommand answered 2/7 at 3:2 Comment(1)
No. Don't mess with /usr/bin. If you want this, put the symlink in /usr/local/bin/pythonRopedancer

© 2022 - 2024 — McMap. All rights reserved.