简体   繁体   中英

Install numpy in Python virtualenv

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

I have the error when installing numpy in the virtualenv.

Maybe, you have any ideas to fix?

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'

make sure apt-get and gcc are up to date

sudo apt-get update    
sudo apt-get upgrade gcc

then install the 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 (eg numpy) within the active env (the env created by virtualenv which you might have even changed directory to while doing all this)

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 -E doesn't work. The following is an excerpt from the release notes of version 1.1 ( https://pip.pypa.io/en/latest/news.html )

Removed -E/--environment option 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 Foo with virtualenv path/to/venv && path/to/venv/pip install Foo

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.

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..."

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.

eg, pip install numpy then python ./setup.py install

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

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

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.

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 -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 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

$ 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

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

sudo apt-get install python-dev

You can also install it via the Software Center:

在此输入图像描述

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM