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?

16,356

Solution 1

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)

Solution 2

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.

Solution 3

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

Share:
16,356

Related videos on Youtube

Tian Jiang
Author by

Tian Jiang

Updated on September 15, 2022

Comments

  • Tian Jiang
    Tian Jiang over 1 year

    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.

    • Blender
      Blender almost 9 years
      You need to run the right pip. You should have pip2 and pip3. I'm not sure what Anaconda uses.
  • Tian Jiang
    Tian Jiang almost 9 years
    Anaconda would be expected to hijack pip, but interestingly mine is defaulting to Python 2.7. I re-installed pip from pip.pypa.io/en/stable/installing.html#install-pip, by running the "get-pip.py" file in Python 3.4 (IDLE), and receiving verification that pip is installed in my Python 3.4's /lib/site-packages folder. But both pip install and pip3 install commands in the command prompt are still defaulting to Python 2.7, and telling me that the requirement is already satisfied. So, I'm still unable to install packages to Python 3.4 (IDLE).
  • B.Mr.W.
    B.Mr.W. almost 9 years
    @TianJiang run the command which pip3 and open the file path in an editor like VIM, change the #! to point to Python3.4 instead of Python2.7. Let me know if that works.
  • Tian Jiang
    Tian Jiang almost 9 years
    Thanks. which pip3 is giving me this error: "DNS server not authoritative for zone". (I don't know what that means. Following one suggestion on the internet, I ran sfc /scannow on cmd, but it didn't do the job for me.) Is there possibly another way I could find that path for pip3?
  • Alex Punnen
    Alex Punnen about 7 years
    Even though pip and python is pointing to Anaconda installaiton, pip install (of a biggish library) is installing to system python. alex@alex-Lenovo-G400s-Touch:~/Coding/IPythoN$ which pip /home/alex/anaconda3/bin/pip alex@alex-Lenovo-G400s-Touch:~/Coding/IPythoN$ cat /home/alex/anaconda3/bin/pip #!/home/alex/anaconda3/bin/python if name == 'main': import sys import pip