Command Python2 not found

2020-04-29 16:58发布

问题:

I have to use Python2 for the following command: python2 -m pip install SomePackage in the command line. I get the message that Python2is not found, but I have definitly installed Python 2.7.1.

When I run python --version I get the output Python 3.5.1.

Edit: I use Windows. And the commands whereis and env were also not found.

回答1:

Under windows you have to use:

py -2 yourfilename  // for python2.x
py -3 yourfilename  // for python3.x


回答2:

If you really have installed python2.x and it is on your path, you can ensure that you are installing for python2 by running

pip2 install somepackage

Equivalently you can run

pip3 install somepackage

to ensure that it is installed on python3.x.

This can become a bit messy/tedious in the long run, so it might be worth looking into using virtual environments, or something like miniconda which tend to handle this quite well.



回答3:

The canonical way to find out where a command is found on the path with with the Bourne shell built-in,

$ command -v python
/usr/local/anaconda/bin/python

(BTW, don't use which; let the shell tell you what it's doing.)

It could easily be that Python2 is on your path, but later in the list than the one that's being found. It could also be that the shell's cache of found executables needs updating:

$ help hash
hash: hash [-lr] [-p pathname] [-dt] [name ...]
Remember or display program locations.
...
  -d                forget the remembered location of each NAME

$ hash -d python; command -v python
/usr/local/anaconda/bin/python

To display the path in a more friendly way:

$ echo $PATH | tr :  \\n 
/usr/local/anaconda/bin
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/usr/games
/usr/local/games

You may want to re-arrange your path. Another trick I sometimes use is to rename the system-provided executable, perhaps by capitalizing it, so it's still available but won't be found without special effort.