Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble getting Virtualenv to Import Numpy

I am having trouble with all libraries, but let me focus on numpy. If I am outside a virtualenv, I can go into the Python interpreter and do:

import numpy

and that works. But if I got into a virtualenv and try it:

$ workon test
(test):~/Project/test$ python

>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named numpy

After hours of Googling I believe the problem must be in my understanding of virtualenv and virtualenvwrapper. I have tried the obvious:

(test):~/Projects/test$ pip install numpy

but I got the error:

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

I also tried doing sudo apt-get install python-dev but got the errors:

The following packages have unmet dependencies:
 python-dev : Depends: python2.7-dev (>= 2.7.3) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

I am running Python2.7.3 on Ubuntu 12.04 and have PyDev (no virtualenv) running with a project that currently imports libraries (like numpy) with no trouble. I have tried using the Ubuntu Software Center to install python-dev, but I get the same errors.

like image 863
john_science Avatar asked Apr 15 '26 22:04

john_science


1 Answers

virtualenv by default doesn't let you import packages from the global environment. Use [mk]virtualenv --system-site-packages to allow it to import system packages.

python-dev is a system package, so the pip error is expected. Not sure about the apt error, but you could ask on askubuntu.com to try to resolve it.

like image 177
Danica Avatar answered Apr 17 '26 11:04

Danica