Importing Libraries Issue - “ImportError: No Module named ____”

有些话、适合烂在心里 提交于 2019-11-28 23:46:08

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

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")
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!