18

I have tried importing NumPy in Python, but it did not succeed:

>>> import numpy as np
    x=np.array([[7,8,5],[3,5,7]],np.int32)

   Traceback (most recent call last):
   File "<pyshell#3>", line 1, in <module>
   import numpy as np
   File "C:\Python27\lib\numpy\__init__.py", line 127, in <module>
   raise ImportError(msg)
   ImportError: Error importing numpy: you should not try to import numpy from
   its source directory; please exit the numpy source tree, and relaunch
   your Python interpreter from there.

How can I fix this?

hafizul asad
  • 527
  • 2
  • 5
  • 11

2 Answers2

20

The message is fairly self-explanatory; your working directory should not be the NumPy source directory when you invoke Python; NumPy should be installed and your working directory should be anything but the directory where it lives.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Wooble
  • 87,717
  • 12
  • 108
  • 131
  • how to change my working directory. I know how to do it in matlab, but i am a new user of python and dont know how to do it. – hafizul asad Aug 10 '12 at 17:51
  • 1
    In your terminal, make sure you're in some other directory before you launch python. As long as the numpy folder is living somewhere that is part of your system's PYTHONPATH variable, you can import numpy in python from anywhere on your system. If you're not sure what all of that means, google PYTHONPATH and importing in python. – DaveTheScientist Aug 10 '12 at 17:55
  • i tried this but didnt work; ,>>> os.getcwd() 'C:\\Python27' >>> os.chdir('C:\working directory') >>> import numpy – hafizul asad Aug 10 '12 at 18:08
  • `cd ../..` and then open the python shell. It's saying you can't import `numpy` from inside of `C:\Python27\lib\numpy\` – Ryan Haining Aug 10 '12 at 19:13
1

On Debian/Ubuntu:

aptitude install python-numpy

On Windows, download the installer:

http://sourceforge.net/projects/numpy/files/NumPy/

On other systems, download the tar.gz and run the following:

$ tar xfz numpy-n.m.tar.gz
$ cd numpy-n.m
$ python setup.py install
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shashank
  • 1,133
  • 2
  • 10
  • 24