Python 导入库问题 - “ImportError: No Module named ____”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14311601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:03:06  来源:igfitidea点击:

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

pythonnumpylibrariespipnltk

提问by NeelPatel

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.

我使用 pip 下载了 numpy 和 nltk,根据消息,我知道安装位置是: Requirement already satisfied (use --upgrade to upgrade): nltk in /usr/local/lib/python2.7/site-packages,所以它看起来像是安装在 2.7 版的目录中。

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

当我运行时,python我得到Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43),所以这显然也是 2.7 版。

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

但是,当我在 Python 控制台中尝试“import nltk”或“import numpy”时,总是ImportError: No module named nltk出现错误。任何建议将不胜感激!

采纳答案by Bmayer0122

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.

尝试更改PYTHONPATH环境变量。如果您使用的是 BASH,下面应该可以工作。其他 Linux shell 在分配环境变量的方式上会略有不同。

export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages

回答by Maciek D.

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

问题是它/usr/local/lib/python2.7/site-packages不在您的默认路径列表中。为了验证这一点,请运行以下命令:

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-packagesin the list, you have the following options:

您将获得搜索模块的目录列表。由于您可能不会/usr/local/lib/python2.7/site-packages在列表中,因此您有以下选择:

  1. Remove nltkand 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/.bashrcfile).

  3. Put a file local.pthin /usr/lib/python2.7/site-packagesor /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")
    
  1. nltk在目录路径之一中删除并再次安装它(注意,例如在 Debian 上,它可能是/usr/local/lib/python2.7/dist-packages.

  2. 在每次运行时,设置 PYTHONPATH 变量:(export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages您可以将此命令放在$HOME/.bashrc文件中)。

  3. 将文件local.pth放入/usr/lib/python2.7/site-packages/usr/lib/python2.7/dist-packages(取决于上面脚本的输出),其中包含一行:

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

    这会将此目录永久添加到您的默认路径列表中。

  4. (仅推荐用于安装在某些奇怪位置的一些很少使用的非标准包,这可能不是您的情况)在脚本的开头(之前import nltk)添加以下代码:

    import sys
    sys.path.append("/usr/local/lib/python2.7/site-packages")