简体   繁体   English

找不到命令Python2

[英]Command Python2 not found

I have to use Python2 for the following command: python2 -m pip install SomePackage in the command line. 我必须使用Python2来执行以下命令: python2 -m pip install SomePackage在命令行中python2 -m pip install SomePackage I get the message that Python2 is not found, but I have definitly installed Python 2.7.1. 我得到的信息是Python2 ,但我Python2安装了Python 2.7.1。

When I run python --version I get the output Python 3.5.1 . 当我运行python --version我得到输出Python 3.5.1

Edit: I use Windows. 编辑:我使用Windows。 And the commands whereis and env were also not found. 并且找不到命令whereisenv

Under windows you have to use: 在windows下你必须使用:

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

If you really have installed python2.x and it is on your path , you can ensure that you are installing for python2 by running 如果你真的安装了python2.x并且它在你的path ,你可以确保你通过运行安装python2

pip2 install somepackage

Equivalently you can run 等效地你可以跑

pip3 install somepackage

to ensure that it is installed on python3.x. 确保它安装在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. 从长远来看,这可能会变得有点混乱/繁琐,因此可能值得研究使用虚拟环境,或者像miniconda这样的东西,它们可以很好地处理这个问题。

The canonical way to find out where a command is found on the path with with the Bourne shell built-in, 通过内置Bourne shell查找路径上命令的位置的规范方法,

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

(BTW, don't use which ; let the shell tell you what it's doing.) (顺便说一句,不要使用 ;让shell告诉你它在做什么。)

It could easily be that Python2 is on your path, but later in the list than the one that's being found. 可能很容易就是Python2在你的路径上,但在列表后面而不是正在找到的那个。 It could also be that the shell's cache of found executables needs updating: 也可能是shell的已发现可执行文件的缓存需要更新:

$ 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. 我有时使用的另一个技巧是重命名系统提供的可执行文件,可能通过大写它,所以它仍然可用,但没有特别的努力就找不到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM