10

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!

NeelPatel
  • 103
  • 1
  • 2
  • 5
  • 1
    It seems you have multiple python installations, and `nltk` is installed in "the other python." Changing `PYTHONPATH` is an ugly trick. Take a look at this answer and see if that's your situation: http://stackoverflow.com/a/12206868/565999 – Hugo Lopes Tavares Jan 15 '13 at 22:18

2 Answers2

19

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
Bmayer0122
  • 2,138
  • 2
  • 14
  • 7
  • I had to export the .../dist-packages to make nltk and some other packages accessible via python terminal. Thanks for this hint. – So S Jan 26 '16 at 23:44
  • changing the PYTHONPATH also worked python34 C:\Python34\Lib\site-packages (the path was shown in the list given by import sys for pth in sys.path:print pth but somehow it did not work until I edited the pythonpath – JinSnow Nov 18 '16 at 20:04
10

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")
    
Maciek D.
  • 2,754
  • 1
  • 20
  • 17