在 Virtualenv 环境中安装 python-numpy [英] Install python-numpy in the Virtualenv environment

查看:47
本文介绍了在 Virtualenv 环境中安装 python-numpy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 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.

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

You should not be 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, pip will make changes to your global site packages, not to your virtual environment, hence why you weren't able to install numpy locally.

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

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.

你可以:

  1. 在创建 virtualenv 时传递 --no-site-packages 选项.这可以防止新的 virtualenv 从全局站点包继承,因此所有内容都必须在本地安装.

  1. Pass the --no-site-packages option when you create your virtualenv. This prevents the new virtualenv 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆