在安装了 python 2.7 和 3.5 的 Windows 上使用 pip
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39851566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using pip on Windows installed with both python 2.7 and 3.5
提问by user781486
I am using Windows 10. Currently, I have Python 2.7 installed. I would like to install Python 3.5 as well. However, if I have both 2.7 and 3.5 installed, when I run pip
, how do I get the direct the package to be installed to the desired Python version?
我使用的是 Windows 10。目前,我安装了 Python 2.7。我也想安装 Python 3.5。但是,如果我同时安装了 2.7 和 3.5,那么当我运行 时pip
,如何将要安装的包直接安装到所需的 Python 版本?
采纳答案by Farhan.K
You will have to use the absolute path of pip
.
您将不得不使用pip
.
E.g: if I installed python 3 to C:\python35
, I would use:
C:\> python35\Scripts\pip.exe install packagename
例如:如果我将 python 3 安装到C:\python35
,我会使用:
C:\> python35\Scripts\pip.exe install packagename
Or if you're on linux, use pip3 install packagename
或者,如果您使用的是 linux,请使用 pip3 install packagename
If you don't specify a full path, it will use whichever pip
is in your path
.
如果您不指定完整路径,它将使用pip
您的path
.
回答by Akbar Noto
Because usually i change my intepreter to run something(i got 2 diff projects with both 2 and 3), i use these solution:
因为通常我会改变我的解释器来运行一些东西(我有 2 个 2 和 3 的 diff 项目),我使用这些解决方案:
- Add path to the environment as usual (of course)
- Rename ur python.exe , in my case i want to run python 3 using command python3 on my cmd. So i renamed my python.exe in python3.x directory with python3. Itll works with python 2 ofc.
- Then to use pip in both python, i use this command.
- 像往常一样添加环境路径(当然)
- 重命名你的 python.exe ,在我的情况下,我想在我的 cmd 上使用命令 python3 运行 python 3。所以我用python3在python3.x目录中重命名了我的python.exe。它适用于 python 2 ofc。
- 然后在两个python中使用pip,我使用这个命令。
python3 -m pip install 'somepackage'
python3 -m pip install 'somepackage'
and to run pip on python2
并在 python2 上运行 pip
python -m pip install 'somepackage'
This is may not the best solution out there, but i like this one
这可能不是最好的解决方案,但我喜欢这个
参考:https: //datascience.com.co/how-to-install-python-2-7-and-3-6-in-windows-10-add-python-path-281e7eae62a
回答by Daniel F.
In my case, I have Python 2.7 and Python 3.4, with the Python Launcher for Windows.
就我而言,我有 Python 2.7 和 Python 3.4,以及适用于 Windows 的 Python Launcher。
This is the output when running this commands:
这是运行此命令时的输出:
PS C:\> pip -V
pip 9.0.1 from c:\python27\lib\site-packages (python 2.7)
PS C:\> pip3 -V
pip 9.0.1 from C:\Python34\lib\site-packages (python 3.4)
I'll note that in my Python27\Scripts\
directory, I have pip.exe
, pip2.exe
and pip2.7.exe
.
And in my Python34\Scripts\
directory, I have pip.exe
, pip3.exe
and pip3.4.exe
.
So all of these .exe files help you when you have different versions of Python installed at the same time.
我会注意到,在我的Python27\Scripts\
目录中,我有pip.exe
,pip2.exe
和pip2.7.exe
.
在我的Python34\Scripts\
目录中,我有pip.exe
,pip3.exe
和pip3.4.exe
.
因此,当您同时安装不同版本的 Python 时,所有这些 .exe 文件都会为您提供帮助。
Of course, for this to work, you have to have the respective Scripts
directries in your Path
system enviroment variable.
当然,要使其正常工作,您必须Scripts
在Path
系统环境变量中拥有相应的目录。
回答by user781486
The answer from Farhan.K will work. However, I think a more convenient way would be to rename python35\Scripts\pip.exe
to python35\Scripts\pip3.exe
assuming python 3 is installed in C:\python35
.
Farhan.K 的答案会奏效。但是,我认为更方便的方法是重命名python35\Scripts\pip.exe
为python35\Scripts\pip3.exe
假设 python 3 安装在C:\python35
.
After renaming, you can use pip3
when installing packages to python v3 and pip
when installing packages to python v2. Without the renaming, your computer will use whichever pip is in your path.
重命名后可以pip3
在安装包到python v3和pip
安装包到python v2时使用。如果没有重命名,您的计算机将使用您路径中的任何 pip。
回答by sinoroc
I would advise against ever calling any pip
script directly (nor pip3
, pip2.7.exe
, anything like that).
我会建议对曾经调用任何pip
直接的脚本(也没有pip3
,pip2.7.exe
,类似的东西)。
Instead, a surefire way is to always prefer the explicit variant of calling pip's executable module for a specific Python interpreter:
相反,一种可靠的方法是始终更喜欢为特定的 Python 解释器调用pip的可执行模块的显式变体:
path/to/pythonX.Y -m pip somecommand
path/to/venv/bin/python -m pip somecommand
C:\path\to\venv\Scripts\python.exe -m pip somecommand
path/to/pythonX.Y -m pip somecommand
path/to/venv/bin/python -m pip somecommand
C:\path\to\venv\Scripts\python.exe -m pip somecommand
There are many advantages to this, for example:
这样做有很多好处,例如:
- It is explicit for which Python interpreter the projects will be pip-installed(Python 2 or 3, inside the virtual environment or not, etc.)
- For a virtual environment, one can pip-install(or do other things) without activating it:
path/to/venv/bin/python -m pip install SomeProject
- Under Windows this is the only way to safely upgrade pipitself
path\to\venv\Scripts\python.exe -m pip install --upgrade pip
- 项目将安装 pip 的Python 解释器是明确的(Python 2 或 3,在虚拟环境中与否,等等)
- 对于虚拟环境,可以在不激活它的情况下进行pip-install(或执行其他操作):
path/to/venv/bin/python -m pip install SomeProject
- 在 Windows 下,这是安全升级pip本身的唯一方法
path\to\venv\Scripts\python.exe -m pip install --upgrade pip
But yes, if all is perfectly setup, then python3 -m pip install SomeProject
and pip3 install SomeProject
should do the exact same thing, but there are way too many cases where there is an issue with the setup and things don't work as expected and users get confused (as shown by the many questions about this topic on this platform).
但是,是的,如果一切都完美设置,那么python3 -m pip install SomeProject
并且pip3 install SomeProject
应该做完全相同的事情,但是有太多情况下设置存在问题并且事情没有按预期工作并且用户感到困惑(如在这个平台上有很多关于这个话题的问题)。
References
参考
- Brett Cannon's article "Why you should use
python -m pip
" - pip's documentation section on "Upgrading pip"
- venv's documentation section on "Creating virtual environments": "You don't specifically need to activate an environment[...]"
回答by g-io
I ran across an issue with running pip with absolute path. This might be related to WinPython's installation routine and the order of installing Python 3.6 first, 2.7 second, or Python 3.6 being in the path.
我遇到了使用绝对路径运行 pip 的问题。这可能与 WinPython 的安装例程和 Python 3.6 先安装、2.7 秒或 Python 3.6 安装顺序有关。
No matter which pip was called, it was activating the 3.6 one:
无论调用哪个点子,它都会激活 3.6 点:
λ C:\prog\WinPython-64bit-2.7.13.1Zero\python-2.7.13.amd64\Scripts\pip2.exe --version
pip 9.0.1 from C:\prog\WinPython-64bit-3.6.1.0Zero\python-3.6.1.amd64\lib\site-packages (python 3.6)
What finally did the trick was calling pip as a module of the respective python binary:
最终的诀窍是将 pip 作为相应 python 二进制文件的模块调用:
λ C:\prog\WinPython-64bit-2.7.13.1Zero\python-2.7.13.amd64\python.exe -m pip --version
pip 9.0.1 from C:\prog\WinPython-64bit-2.7.13.1Zero\python-2.7.13.amd64\lib\site-packages (python 2.7)
Hope that might help someone with similar issues.
希望可以帮助有类似问题的人。
回答by ??? ????????
1-open command prompt and change direction using the command cd C:\Python35\Scripts
1-打开命令提示符并使用命令更改方向 cd C:\Python35\Scripts
2- write the command pip3 install --upgrade pip
2-写命令 pip3 install --upgrade pip
3- close the command prompt and reopen it again to return to the default direction and use the command pip3.exe install package_name
to install any package you want
3-关闭命令提示符并再次重新打开以返回默认方向并使用该命令pip3.exe install package_name
安装您想要的任何包
回答by user703978
I tried many things , then finally
pip3 install --upgrade pip
worked for me as i was facing this issue since i had both python3 and python2.7 installed on my system.
mind the pip3 in the beginning and pip in the end.
And yes you do have to run in admin mode the command prompt and make sure if the path is set properly.
我尝试了很多东西,然后终于
pip3 install --upgrade pip
为我工作,因为我面临这个问题,因为我的系统上安装了 python3 和 python2.7。注意开头的 pip3 和结尾的 pip。是的,您必须在管理员模式下运行命令提示符并确保路径设置正确。