我想在 Virtualenv 环境中安装 python-numpy。我的系统是Ubuntu 12.04,我的python是2.7.5。首先我安装了 Virtualenv
$ 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
$ sudo apt-get install python-numpy
但是,在完成上述所有步骤后,我尝试在环境中导入 numpy in python 。 Python 告诉我“没有名为 numpy 的模块”。而 numpy 可以在 Python 中全局导入。我尝试了多次删除和安装,但都不起作用。我是 Python 和 Linux 的初学者。
apt-get
即使您在新的环境中,仍然会全局安装模块virtualenv
.
你应该使用pip install numpy
从您的虚拟环境中(最简单的方法),或者编译并安装numpy
从源头使用setup.py
文件位于源目录的根目录中(稍微困难一点的方法,see here http://docs.scipy.org/doc/numpy/user/install.html#building-from-source).
我也强烈推荐你看一下virtualenvwrapper http://virtualenvwrapper.readthedocs.org/en/latest/,这使得管理虚拟环境变得更加友好。
Edit:
你应该not正在使用sudo
,无论是创建虚拟环境还是在其中安装东西 - 它是您的主文件夹中的一个目录,您不需要提升权限即可对其进行更改。如果你使用sudo
, pip
将对您的全局站点包进行更改,而不是对您的虚拟环境进行更改,因此您无法安装numpy
本地。
Another thing to consider is that by default, new virtualenvs
will 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 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.
你可以:
通过--no-site-packages
当你创建你的选项virtualenv
。这可以防止新的virtualenv
从全局站点包继承,因此所有内容都必须在本地安装。
Force pip
安装/升级numpy
本地,例如使用pip install -U --force numpy
* 从 v1.7 开始 https://github.com/pypa/virtualenv/blob/develop/docs/changes.rst#17-2011-11-30,默认行为virtualenv
是不包括全球site-packages
目录。您可以通过传递来覆盖它--system-site-packages
创建新的虚拟环境时进行标记。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)