Search code examples
pythonimportnumpymatplotlibdirectory

Explain why numpy should not be imported from source directory


Disclaimer of research:

I have examined the following other StackOverflow questions:

Perhaps to some, those may answer my question, but according to my knowledge, I still do not understand the situation.

I am trying to import numpy so that matplotlib will work, but upon execution of the __init__.py file in the numpy folder, the following error message is displayed:

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 intepreter from there.

Explain what it means to import something from its source directory as opposed to some other way of importing it. Does it mean that it should not be source code when it is imported? Or does it mean that it literally is just the wrong directory/folder that I am importing. I know that one other StackOverflow answer is:

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.

However, I don't understand this. Aren't you supposed to import things that you want to work with? I'm assuming that the import command combines the source directory into your current working directory in this statement.

I also read the other answers such as:

  • Using distutils to install local directories

  • Using virtualenv to create a virtual system directory

  • Using Enthought's EPD to have numpy pre-installed in what I believe to be the system directory, and

  • Using a command like $ dpkg -i --force-not-root --root=$HOME mypackagename.deb to create what I believe is some kind of sub-system directory that is treated like a system directory.

So, correct me if I'm wrong, but does numpy somehow strongly require to be somehow installed in the main system directory?

Machine status:

I am using Windows machines without administrative privlidges. They have Python 3.3 Shell as well as matplotlib installed. When running command prompt, python and python3 are not recognized. I have to run the Python shell from the applications menu. I can successfull begin importing matplotlib from even my own directory, different from theirs, but it stops upon reaching __init__.py of the numpy module, if it exists and reports the error stated above.

Update:

Luckily, my administrators were able to directly install numpy correctly in the site-packages folder. Thank you for answering my question though. I understand the situation a lot more because of you.


Solution

  • numpy includes extension modules written in C. You will need to build these extension modules before the numpy package is complete. The most robust way to do this is to build it and install it to site-packages like normal. You can also install it to another directory using the standard distutils options for this. However, once you have installed it, you should change your directory out of the source tree. Python starts looking for packages in your current directory, so the presence of the incomplete numpy package (without the necessary built C extension modules) will be picked up first and lead to the error that message that you quote. This happens a lot, so we give a long message explaining what to do.