在 Python virtualenv 中安装 numpy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18785063/
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 11:50:32  来源:igfitidea点击:

Install numpy in Python virtualenv

pythonubuntunumpyvirtualenv

提问by karavanjo

I've created virtualenv for Python 2.7.4 on Ubuntu 13.04. I've installed python-dev.

我在 Ubuntu 13.04 上为 Python 2.7.4 创建了 virtualenv。我已经安装了 python-dev。

I have the errorwhen installing numpy in the virtualenv.

我有错误的virtualenv中安装numpy的时候。

Maybe, you have any ideas to fix?

也许,你有什么想法要解决?

采纳答案by samkhan13

The problem is SystemError: Cannot compile 'Python.h'. Perhaps you need to install python-dev|python-devel.

问题是 SystemError: Cannot compile 'Python.h'. Perhaps you need to install python-dev|python-devel.

so do the following in order to obtain 'Python.h'

因此请执行以下操作以获得“Python.h”

make sure apt-get and gcc are up to date

确保 apt-get 和 gcc 是最新的

sudo apt-get update    
sudo apt-get upgrade gcc

then install the python2.7-dev

然后安装python2.7-dev

sudo apt-get install python2.7-dev

and I see that you have most probably already done the above things.

我看到你很可能已经完成了上述事情。

pip will eventually spit out another error for not being able to write into /user/bin/blahBlah/dist-packages/or something like that because it couldn't figure out that it was supposed to install your desiredPackage (e.g. numpy) within the active env (the env created by virtualenv which you might have even changed directory to while doing all this)

pip 最终会因为无法写入/user/bin/blahBlah/dist-packages/或类似的东西而吐出另一个错误,因为它无法弄清楚它应该在活动环境(由 virtualenv 创建的环境)中安装您想要的包(例如 numpy)在执行所有这些操作时甚至将目录更改为)

so do this:

所以这样做:

pip -E /some/path/env install desiredPackage

that should get the job done... hopefully :)

这应该可以完成工作......希望:)

---Edit---

- -编辑 - -

From PIP Version 1.1 onward, the command pip -Edoesn't work. The following is an excerpt from the release notes of version 1.1 (https://pip.pypa.io/en/latest/news.html)

从 PIP 版本 1.1 开始,该命令pip -E不起作用。以下是 1.1 版发行说明的摘录(https://pip.pypa.io/en/latest/news.html

Removed -E/--environmentoption and PIP_RESPECT_VIRTUALENV; both use a restart-in-venv mechanism that's broken, and neither one is useful since every virtualenv now has pip inside it. Replace pip -E path/to/venv install Foowith virtualenv path/to/venv && path/to/venv/pip install Foo

删除了-E/--environment选项和PIP_RESPECT_VIRTUALENV;两者都使用了一种已损坏的 restart-in-venv 机制,而且都没有用,因为每个 virtualenv 现在都在其中包含 pip。替换pip -E path/to/venv install Foovirtualenv path/to/venv && path/to/venv/pip install Foo

回答by Games Brainiac

This is probably because you do not have the python-devpackage installed. You can install it like this:

这可能是因为您没有安装该python-dev软件包。您可以像这样安装它:

sudo apt-get install python-dev

You can also install it via the Software Center:

您也可以通过软件中心安装它:

enter image description here

在此处输入图片说明

回答by nebffa

If you're on Python3 you'll need to do sudo apt-get install python3-dev. Took me a little while to figure it out.

如果您使用的是 Python3,则需要执行sudo apt-get install python3-dev. 我花了一点时间才弄明白。

回答by drpoo

If you're hitting this issue even though you've installed all OS dependencies (python-devel, fortran compiler, etc), the issue might be instead related to the following bug: "numpy installation thru install_requires directive issue..."

如果您在安装了所有操作系统依赖项(python-devel、fortran 编译器等)后仍遇到此问题,则该问题可能与以下错误有关: “numpy 安装通过 install_requires 指令问题...”

Work around is to manually install numpy in your (virtual) environment before running setup.py to install whatever you want to install that depends on numpy.

解决方法是在运行 setup.py 之前在您的(虚拟)环境中手动安装 numpy 以安装您想要安装的任何依赖于 numpy 的东西。

eg, pip install numpythen python ./setup.py install

例如,pip install numpy那么python ./setup.py install

回答by gevra

@samkhan13 solution didn't work for me as pip said it doesn't have the -E option. I was still getting the same error, but what worked for me was to install matplotlib, which installed numpy.

@samkhan13 解决方案对我不起作用,因为 pip 说它没有 -E 选项。我仍然遇到同样的错误,但对我有用的是安装 matplotlib,它安装了 numpy。

回答by lukecampbell

This answer is for those of us that compiled python from source or installed it to a non standard directory. In my case, python2.7 was installed to /usr/local and the include files were installed to /usr/local/include/python2.7

这个答案适用于我们这些从源代码编译 python 或将其安装到非标准目录的人。就我而言,python2.7 安装到 /usr/local 并且包含文件安装到 /usr/local/include/python2.7

C_INCLUDE_PATH=/usr/local/include/python2.7:$C_INCLUDE_PATH pip install numpy

回答by Alex Volkov

I recently had the same problem. I run Debian Jessie and tried to install numpy from a Python 2.7.9 virtualenv. I got the same error -- numpy complaining that Python.h is missing while python2.7-dev and gcc are already installed.

我最近遇到了同样的问题。我运行 Debian Jessie 并尝试从 Python 2.7.9 virtualenv 安装 numpy。我遇到了同样的错误——numpy 抱怨 Python.h 丢失,而 python2.7-dev 和 gcc 已经安装。

File "numpy/core/setup.py", line 42, in check_types
],
File "numpy/core/setup.py", line 293, in check_types

SystemError: Cannot compile 'Python.h'. Perhaps you need to install python-dev|python-devel.

I'm running pip 1.5.6 and it doesn't appear to have command line option '-E'

我正在运行 pip 1.5.6,它似乎没有命令行选项“-E”

$ pip -V
pip 1.5.6 from /home/alex/.virtualenvs/myenv/local/lib/python2.7/site-  packages (python 2.7)

Upgrading pip to the latest verson 7.0.3 solves the problem

将pip升级到最新版本7.0.3即可解决问题

$ pip install --upgrade pip
Downloading/unpacking pip from https://pypi.python.org/packages/py2.py3/p/pip/pip-7.0.3-py2.py3-none-any.whl#md5=6950e1d775fea7ea50af690f72589dbd
Downloading pip-7.0.3-py2.py3-none-any.whl (1.1MB): 1.1MB downloaded
Installing collected packages: pip
Found existing installation: pip 1.5.6
Uninstalling pip:
  Successfully uninstalled pip
Successfully installed pip
Cleaning up...

Now it is possible to install numpy

现在可以安装 numpy

$ pip install numpy
Collecting numpy
Downloading numpy-1.9.2.tar.gz (4.0MB)
100% |████████████████████████████████| 4.0MB 61kB/s
Installing collected packages: numpy
Running setup.py install for numpy

Successfully installed numpy-1.9.2