简体   繁体   English

当我的计算机上有 Python 2、Python 3 和 Anaconda 时,如何控制将包安装到哪个 Python 发行版?

[英]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:我的 Windows 计算机上安装了以下 Python 发行版:

  • Python 2.7 (IDLE) Python 2.7(空闲)
  • Python 3.4 (IDLE) Python 3.4(空闲)
  • Anaconda (Python 3.4)蟒蛇(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.例如,现在,我正在尝试将pytz安装到 Python 3.4 (IDLE),而pip install似乎默认为 Python 2.7 (IDLE),这是我在计算机上安装的第一个 Python 发行版。

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.如果您安装了 Anaconda python,它可能会覆盖 python 命令以默认指向 Anaconda 解释器,pip 也是如此。 In that case, all the libraries installed by pip command will be installed under the Anaconda python library path:在这种情况下,所有通过pip命令安装的库都将安装在 Anaconda python 库路径下:

$ 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默认 Python2.7

If you try to install libraries under default Python2.7, you can specify the pip path like this:如果您尝试在默认 Python2.7 下安装库,您可以像这样指定 pip 路径:

/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.在这种情况下,它将使用 Python2.7 解释器来编译库并将其安装在默认的 Python2.7 库文件夹下。

Python3蟒蛇3

In my Ubuntu VM, python3 is installed as default but not the pip3.在我的 Ubuntu VM 中,python3 是默认安装的,但没有安装 pip3。 I have to install by doing sudo apt-get install python3-pip .我必须通过执行sudo apt-get install python3-pip After it is installed, you can use pip3 to install libraries for python3.安装好之后就可以使用pip3来安装python3的库了。

More about PIP ( ReadTheFullManual ):更多关于 PIP ( ReadTheFullManual ):

There are indeed a lot of interesting arguments in pip command itself to let you install package in whatever way you like. pip 命令本身确实有很多有趣的参数,可以让你以任何你喜欢的方式安装包。

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) pip install --target将在指定的库中安装库,您实际上可以使用 Anaconda pip 将库安装到默认的 python 库下......(不知道为什么会有人这样做)

I'm not sure why you need so many different Pythons, but for Anaconda, you should use conda.我不确定为什么你需要这么多不同的 Python,但是对于 Anaconda,你应该使用 conda。

conda install pytz

will install pytz into your Anaconda Python.pytz安装到您的 Anaconda Python 中。

If all you are aiming to do is to have both Python 2 and Python 3 you can do this with conda.如果您的目标是同时拥有 Python 2 和 Python 3,您可以使用 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.将使用 Python 2.7 版本的 Anaconda 创建 conda 环境(类似于 virtualenv 但功能更强大)。 You can then activate this with然后你可以激活它

activate py27

See http://continuum.io/blog/anaconda-python-3 .请参阅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.您也可以将pip与 Anaconda 一起使用,但建议使用 conda,除非您需要的包无法通过 conda 获得。

For Anaconda go to C:\\Users\\USERNAME\\Anaconda3\\Scripts对于 Anaconda,请转到 C:\\Users\\USERNAME\\Anaconda3\\Scripts

Change these files pip-script.py and pip.exe to pip3-script.py and pip3.exe .将这些文件pip-script.pypip.exe更改为pip3-script.pypip3.exe 在此处输入图片说明

Then add these variables to your system variables.然后将这些变量添加到您的系统变量中。

在此处输入图片说明

Voila..!!瞧……!! Your Job is done.你的工作完成了。 Now to install use pip2 for 2.7 and pip3 for anaconda version.现在安装使用 pip2 for 2.7 和 pip3 for anaconda 版本。 在此处输入图片说明

(in command prompt)C:\\Python34\\scripts\\pip.exe install pytz (在命令提示符下)C:\\Python34\\scripts\\pip.exe 安装 pytz

this assumes your path is similar to mine.这假设您的路径与我的相似。 I used the default install location for all my pythons(2.7,3.4).我使用了所有 pythons(2.7,3.4) 的默认安装位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python如果anaconda是我的python组织者,我应该使用pip / pip3来安装/更新软件包吗? - Python If I should use pip/pip3 to install/update packages when anaconda is my python organizer? 我的电脑无法在 Python 3.8 上安装 pip - My computer can not install pip on Python 3.8 当已经有 python 但缺少 pip 时,如何安装 pip? - How can I install pip when there is already python but pip is missed? 我如何在没有pip或virtualenv的情况下安装python软件包 - How can I install a python package without pip or virtualenv 我如何控制要安装软件包的python - how do i control to which python to install a package 我已经在pip中上传了一个Python包,然后pip安装了这个包,但是我在Python Interpreter中无法导入这个包 - I have uploaded a Python package in pip, then pip installed this package, but I can't import this package in Python Interpreter 如何在我的 Mac 上安装 Python 的 pip3? - How can I install Python's pip3 on my Mac? 如何修复我的anaconda python发行版? - How do I fix my anaconda python distribution? 如何手动将软件包安装到 anaconda 的 python 发行版中? - How to manually install a package into anaconda's python distribution? 有2个Python版本时如何使用pip安装软件包 - How to install package using pip when you have 2 Python versions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM