简体   繁体   English

导入错误:“没有命名模块”。 但是模块已经安装在 dist-packages 中

[英]ImportError: “No modules named”. But modules already installed in dist-packages

I am using python2.7 and trying to import modules such as psycopg2 .我正在使用 python2.7 并尝试导入模块,例如psycopg2 But I get the following error when I try to import the module:但是当我尝试导入模块时出现以下错误:

import psycopg2
ImportError: No module named psycopg2

When I try pip to install the module it gives me the following message:当我尝试pip安装模块时,它给了我以下消息:

Requirement already satisfied (use --upgrade to upgrade): psycopg2 in /usr/local/lib/python2.7/dist-packages
Cleaning up...

Can anyone please tell me what I am doing wrong?谁能告诉我我做错了什么?

Is the module installed in your PYTHONPATH?该模块是否安装在您的 PYTHONPATH 中?

You can verify running this command line:您可以验证运行此命令行:

python -c "import sys; print '/usr/local/lib/python2.7/dist-packages' in sys.path"

Try to put psycopg2 module (or package, i don't know psycopg2) in the same directory of your script, and try to import it.尝试将 psycopg2 模块(或包,我不知道 psycopg2)放在脚本的同一目录中,然后尝试导入它。 Import searches first in the current directory.导入首先在当前目录中搜索。

import sys
print sys.path

Should display which are the search directories for the python interpreter, in order from the first to the last.应该按从第一个到最后一个的顺序显示哪些是 python 解释器的搜索目录。 The first is always the current directory, then there are the directories in PYTHONPATH and then python setup-dependent directories.第一个始终是当前目录,然后是 PYTHONPATH 中的目录,然后是依赖于 python setup 的目录。

See: https://docs.python.org/2.7/tutorial/modules.html#the-module-search-path请参阅: https : //docs.python.org/2.7/tutorial/modules.html#the-module-search-path

You can edit sys.path in order to reach your module, or put the module in one of its directories.您可以编辑 sys.path 以访问您的模块,或将模块放入其目录之一。

Make sure that your PYTHONPATH and/or PYTHONHOME variables are set properly.确保您的 PYTHONPATH 和/或 PYTHONHOME 变量设置正确。 These environment/command line variables get searched when Python looks for modules to import.当 Python 查找要导入的模块时,会搜索这些环境/命令行变量。 So, if the module is properly installed, you should make sure a reference that location is in one of those variables.因此,如果模块安装正确,您应该确保该位置的引用位于这些变量之一中。

Check out these links PYTHONHOME and PYTHONPATH查看这些链接PYTHONHOMEPYTHONPATH

Make sure that you are running your program in same python version in which you have installed package确保您在安装软件包的同一 python 版本中运行您的程序

For example,you have installed package in python3 and you are running the code with python2..that might be the case to give the error例如,您已经在 python3 中安装了包,并且您正在使用 python2 运行代码..这可能是给出错误的情况

Check where you installed the package, for me it was into the python 32 bit folder c:\\program files (x86)\\python37-32\\lib\\site-packages .检查您安装包的位置,对我来说,它位于 python 32 位文件夹c:\\program files (x86)\\python37-32\\lib\\site-packages 中

The problem I was running VsCode in x64 bit mode and the packages live in the x86 folder.我在 x64 位模式下运行 VsCode 并且包位于 x86 文件夹中的问题。

See here how you can change the interpreter you're using - in my case - I needed to set it to Python 3.7.4(x86) 32 bit (image off internet):在这里查看如何更改您正在使用的解释器 - 在我的情况下 - 我需要将其设置为 Python 3.7.4(x86) 32 位(互联网上的图像):

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM