Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I control which Python distribution to pip install a package to when I have Python 2, Python 3, and Anaconda on my computer?

I have the following Python distributions installed on my Windows computer:

  • Python 2.7 (IDLE)
  • Python 3.4 (IDLE)
  • Anaconda (Python 3.4)

Obviously, they all store their libraries in different locations.

So, how can I easily make a targeted installation to (a different) one of them each time I need to do so?

For example, right now, I am trying to install pytz to Python 3.4 (IDLE), and pip install seems to be defaulting to Python 2.7 (IDLE), which is the first distribution of Python I had installed on my computer.

like image 353
Tian Jiang Avatar asked May 21 '15 03:05

Tian Jiang


People also ask

How does pip decide where to install?

Once pip has a list of compatible distributions, it sorts them by version, chooses the most recent version, and then chooses the "best" distribution for that version. It prefers binary wheels if there are any, and if they are multiple it chooses the one most specific to the install environment.

Which Python is pip installing to?

PIP is automatically installed with Python 2.7.9+ and Python 3.4+ and it comes with the virtualenv and pyvenv virtual environments. Before you install PIP on Windows, check if PIP is already installed.

Can I install packages for anaconda with pip?

Both pip and conda are included in Anaconda and Miniconda, so you do not need to install them separately.

Does pip install packages for all users?

Passing the --user option to python -m pip install will install a package just for the current user, rather than for all users of the system.


3 Answers

Anaconda Python

If you have Anaconda python installed, it probably will overwrite python command to point to the Anaconda interpreter as default, so does pip. In that case, all the libraries installed by pip command will be installed under the Anaconda python library path:

$ which python
/home/datafireball/anaconda/bin/python
$ which pip
/home/datafireball/anaconda/bin/pip
$ cat /home/datafireball/anaconda/bin/pip
#!/home/datafireball/anaconda/bin/python
if __name__ == '__main__':
    import sys
    from pip import main
sys.exit(main())

Default Python2.7

If you try to install libraries under default Python2.7, you can specify the pip path like this:

/usr/bin/pip install <libraryname>

In that case, it will use the Python2.7 interpreter to compile the library and it will be installed under default Python2.7 library folder.

Python3

In my Ubuntu VM, python3 is installed as default but not the pip3. I have to install by doing sudo apt-get install python3-pip. After it is installed, you can use pip3 to install libraries for python3.

More about PIP (ReadTheFullManual):

There are indeed a lot of interesting arguments in pip command itself to let you install package in whatever way you like.

For example,

pip install --target will install the library in specified library, which you can actually using Anaconda pip to install the library to be under default python library... (not sure why would anyone do this)

like image 77
B.Mr.W. Avatar answered Sep 28 '22 03:09

B.Mr.W.


I'm not sure why you need so many different Pythons, but for Anaconda, you should use conda.

conda install pytz

will install pytz into your Anaconda Python.

If all you are aiming to do is to have both Python 2 and Python 3 you can do this with conda.

conda create -n py27 python=2.7 anaconda

will create a conda environment (similar to a virtualenv but more powerful) with the Python 2.7 version of Anaconda. You can then activate this with

activate py27

See http://continuum.io/blog/anaconda-python-3.

You can also use pip with Anaconda, but using conda is recommended unless the package you need is not available through conda.

like image 44
asmeurer Avatar answered Sep 28 '22 01:09

asmeurer


For Anaconda go to C:\Users\USERNAME\Anaconda3\Scripts

Change these files pip-script.py and pip.exe to pip3-script.py and pip3.exe. enter image description here

Then add these variables to your system variables.

enter image description here

Voila..!! Your Job is done. Now to install use pip2 for 2.7 and pip3 for anaconda version. enter image description here

like image 30
Priyansh Avatar answered Sep 28 '22 03:09

Priyansh