简体   繁体   中英

Using pip on Windows installed with both python 2.7 and 3.5

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?

You will have to use the absolute path of pip .

Eg: if I installed python 3 to C:\\python35 , I would use: C:\\> python35\\Scripts\\pip.exe install packagename

Or if you're on linux, use pip3 install packagename

If you don't specify a full path, it will use whichever pip is in your path .

Because usually i change my intepreter to run something(i got 2 diff projects with both 2 and 3), i use these solution:

  1. Add path to the environment as usual (of course)
  2. 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.
  3. Then to use pip in both python, i use this command.

python3 -m pip install 'somepackage'

and to run pip on python2

python -m pip install 'somepackage'

This is may not the best solution out there, but i like this one

** WINDOWS **

ref : https://datascience.com.co/how-to-install-python-2-7-and-3-6-in-windows-10-add-python-path-281e7eae62a

In my case, I have Python 2.7 and Python 3.4, with the Python Launcher for Windows.

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.

Of course, for this to work, you have to have the respective Scripts directries in your Path system enviroment variable.

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 .

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.

I would advise against ever calling any pip script directly (nor pip3 , pip2.7.exe , anything like that).

Instead, a surefire way is to always prefer the explicit variant of calling pip 's executable module for a specific Python interpreter:

  • 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 pip itself 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).

References

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.

No matter which pip was called, it was activating the 3.6 one:

λ  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:

λ  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.

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.

1-open command prompt and change direction using the command cd C:\\Python35\\Scripts

2- write the command 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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