问题描述
我想在 Virtualenv 环境中安装 python-numpy.我的系统是 Ubuntu 12.04,我的 python 是 2.7.5.首先,我通过
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
$ sudo apt-get install python-virtualenv
然后通过
$ mkdir myproject
$ cd myproject
$ virtualenv venv
New python executable in venv/bin/python
Installing distribute............done.
激活它
$ . venv/bin/activate
在环境中安装了python-numpy
Installed python-numpy in the environment by
$ sudo apt-get install python-numpy
但是,经过上述所有步骤后,我尝试在环境中的python中导入numpy.Python 告诉我没有名为 numpy 的模块".而 numpy 可以在 Python 中全局导入.我多次尝试删除和安装,但它不起作用.我是 Python 和 Linux 的初学者.
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.
推荐答案
apt-get
仍将全局安装模块,即使您使用新的 virtualenv
.
apt-get
will still install modules globally, even when you're in your new virtualenv
.
您应该在虚拟环境中使用 pip install numpy
(最简单的方法),或者使用 setup.py 从源代码编译和安装
numpy
源目录根目录中的文件(稍微难一点,见这里).
You should either use pip install numpy
from within your virtual environment (easiest way), or else compile and install numpy
from source using the setup.py
file in the root of the source directory (slightly harder way, see here).
我还强烈建议您查看 virtualenvwrapper
,这使得管理虚拟环境更加友好.
I'd also thoroughly recommend you take a look at virtualenvwrapper
, which makes managing virtual environments much friendlier.
您应该 *. In your case, since you'd already installed numpy
globally (using apt-get
), when you then try to pip install numpy
in your virtual environment, pip
sees that numpy
is already in your Python path and doesn't install it locally.
你可以:
在创建
virtualenv
时传递--no-site-packages
选项.这可以防止新的virtualenv
从全局站点包继承,因此所有内容都必须在本地安装.
Pass the
--no-site-packages
option when you create yourvirtualenv
. This prevents the newvirtualenv
from inheriting from the global site packages, so everything must be installed locally.
强制 pip
在本地安装/升级 numpy
,例如使用 pip install -U --force numpy
Force pip
to install/upgrade numpy
locally, e.g. using pip install -U --force numpy
* 自 v1.7 起,virtualenv
的默认行为是不包含全局 site-packages
目录.您可以通过在创建新的虚拟环境时传递 --system-site-packages
标志来覆盖它.
* As of v1.7, the default behaviour of virtualenv
is to not include the global site-packages
directory. You can override this by passing the --system-site-packages
flag when creating a new virtual environment.
这篇关于在 Virtualenv 环境中安装 python-numpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!