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?

前端 未结 4 1416
囚心锁ツ
囚心锁ツ 2021-02-08 05:15

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

  • Python 2.7 (IDLE)
  • Python 3.4 (IDLE)
  • Anaconda (Python 3.4)
4条回答
  •  眼角桃花
    2021-02-08 05:27

    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 
    

    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)

提交回复
热议问题