简体   繁体   中英

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

I am using python2.7 and trying to import modules such as .But I get the following error when I try to import the module:

import psycopg2
ImportError: No module named psycopg2

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

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?

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. 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. The first is always the current directory, then there are the directories in PYTHONPATH and then python setup-dependent directories.

See: 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.

Make sure that your PYTHONPATH and/or PYTHONHOME variables are set properly. These environment/command line variables get searched when Python looks for modules to import. 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

Make sure that you are running your program in same python version in which you have installed package

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

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 .

The problem I was running VsCode in x64 bit mode and the packages live in the x86 folder.

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):

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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