Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing Libraries Issue - "ImportError: No Module named ____"

I've looked through a lot of the other question/answers for this topic but no avail.

I downloaded numpy and nltk using pip, and based on the messages I know the install location is: Requirement already satisfied (use --upgrade to upgrade): nltk in /usr/local/lib/python2.7/site-packages, so it looks like it's installing in the directory for version 2.7.

When I run python I get Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43), so that's clearly also version 2.7.

However, when I try "import nltk" or "import numpy" in the Python console, I always get the ImportError: No module named nltk error. Any advice would be greatly appreciated!

like image 586
NeelPatel Avatar asked Jan 14 '13 02:01

NeelPatel


2 Answers

Try changing the PYTHONPATHenvironment variable. If you are using BASH the below should work. Other Linux shells will be slightly different in how they assign environment variables.

export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
like image 159
Bmayer0122 Avatar answered Nov 15 '22 13:11

Bmayer0122


The problem is that /usr/local/lib/python2.7/site-packages is not in your default path list. In order to verify this, run the following commands:

import sys
for pth in sys.path:
    print pth

You will get a list of the directories searched for modules. As you probably will not have /usr/local/lib/python2.7/site-packages in the list, you have the following options:

  1. Remove nltk and install it again in one of the directories paths (note, that e.g. on Debian, it may be /usr/local/lib/python2.7/dist-packages.

  2. On each run, set PYTHONPATH variable: export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages (you can put this command in the $HOME/.bashrc file).

  3. Put a file local.pth in /usr/lib/python2.7/site-packages or /usr/lib/python2.7/dist-packages (depending on the output of the script above), which contains a single line:

    /usr/local/lib/python2.7/site-packages
    

    This will add this directory to your default path list permanently.

  4. (This one is recommended only for some seldom-used non-standard packages installed in some strange location, which is probably not your case) In the beginning of your script (before import nltk) add the following code:

    import sys
    sys.path.append("/usr/local/lib/python2.7/site-packages")
    
like image 20
Maciek D. Avatar answered Nov 15 '22 15:11

Maciek D.