在 Virtualenv 环境中安装 python-numpy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18204134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:12:32  来源:igfitidea点击:

Install python-numpy in the Virtualenv environment

pythonnumpyubuntu-12.04virtualenv

提问by user2677756

I would like to install the python-numpy in the Virtualenv environment. My system is Ubuntu 12.04, and my python is 2.7.5. First I installed the Virtualenv by

我想在 Virtualenv 环境中安装 python-numpy。我的系统是 Ubuntu 12.04,我的 python 是 2.7.5。首先我安装了 Virtualenv

$ sudo apt-get install python-virtualenv

And then set up an environment by

然后通过设置环境

$ mkdir myproject
$ cd myproject
$ virtualenv venv
New python executable in venv/bin/python
Installing distribute............done.

Activated it by

激活它

$ . venv/bin/activate

Installed python-numpy in the environment by

通过在环境中安装 python-numpy

$ sudo apt-get install python-numpy

However, I tried to import numpy in python in the environment after all steps above. Python told me "No modules named numpy". Whereas, numpy could be imported in Python globally. I tried to remove and install many times but it does not work. I am a beginner of both Python and Linux.

但是,经过上述所有步骤后,我尝试在环境中的python中导入numpy。Python 告诉我“没有名为 numpy 的模块”。而 numpy 可以在 Python 中全局导入。我多次尝试删除和安装,但它不起作用。我是 Python 和 Linux 的初学者。

回答by ali_m

apt-getwill still install modules globally, even when you're in your new virtualenv.

apt-get即使您在新的virtualenv.

You should either use pip install numpyfrom within your virtual environment (easiest way), or else compile and install numpyfrom source using the setup.pyfile in the root of the source directory (slightly harder way, see here).

您应该pip install numpy在虚拟环境中使用(最简单的方法),或者numpy使用setup.py源目录根目录中的文件从源编译和安装(稍微困难的方法,请参阅此处)。

I'd also thoroughly recommend you take a look at virtualenvwrapper, which makes managing virtual environments much friendlier.

我还virtualenvwrapper强烈建议您查看,它使管理虚拟环境更加友好。

Edit:

编辑:

You should notbe using sudo, either to create your virtual environment or to install things within it - it's a directory in your home folder, you don't need elevated permissions to make changes to it. If you use sudo, pipwill make changes to your global site packages, not to your virtual environment, hence why you weren't able to install numpylocally.

应该使用sudo, 来创建您的虚拟环境或在其中安装东西 - 它是您的主文件夹中的一个目录,您不需要提升权限来对其进行更改。如果您使用sudo,pip将更改您的全局站点包,而不是您的虚拟环境,因此您无法在numpy本地安装。

Another thing to consider is that by default, new virtualenvswill inherit from the global site-packages- i.e. if Python can't find a module locally within your virtualenv, Python will also look in your global site packages*. In your case, since you'd already installed numpyglobally (using apt-get), when you then try to pip install numpyin your virtual environment, pipsees that numpyis already in your Python path and doesn't install it locally.

另一件要考虑的事情是,默认情况下, newvirtualenvs将从全局继承site-packages- 即如果 Python 无法在您的本地找到模块virtualenv,Python 也会在您的全局站点包* 中查找。在您的情况下,由于您已经numpy全局安装(使用apt-get),当您尝试pip install numpy在您的虚拟环境中安装时,会pip看到它numpy已经在您的 Python 路径中并且不会在本地安装它。

You could:

你可以:

  1. Pass the --no-site-packagesoption when you create your virtualenv. This prevents the new virtualenvfrom inheriting from the global site packages, so everything must be installed locally.

  2. Force pipto install/upgrade numpylocally, e.g. using pip install -U --force numpy

  1. --no-site-packages当你创建你的选项virtualenv。这可以防止新的virtualenv从全局站点包继承,所以所有的东西都必须在本地安装。

  2. 强制pipnumpy本地安装/升级,例如使用pip install -U --force numpy



*As of v1.7, the default behaviour of virtualenvis to not include the global site-packagesdirectory. You can override this by passing the --system-site-packagesflag when creating a new virtual environment.

*从 v1.7 开始, 的默认行为virtualenv是不包含全局site-packages目录。您可以通过--system-site-packages在创建新的虚拟环境时传递标志来覆盖它。

回答by samkhan13

meddling with PYTHONPATH for site-packages indeed defeats the purpose of virtalenv. what worked for me was to specify the env i wanted the packages to be installed in via pip

干预站点包的 PYTHONPATH 确实违背了 virtalenv 的目的。对我有用的是指定我希望通过 pip 安装包的环境

example:

例子:

pip -E /home/proj1 

where proj1was created using virtualenv.

whereproj1是使用virtualenv创建的。

reference: how to install numpy in a virtualenv

参考:如何在 virtualenv 中安装 numpy