简体   繁体   中英

ModuleNotFoundError: No module named 'psycopg2' (python3, mac, pip3 shows psycopg2 successfully installed)

Solved: Learn about virtual environments.

pip install virtualenv


Problem: I get a ModuleNotFoundError for psycopg2 in python3, though it's successfully installed via pip3. (I posted short code to summarize from the terminal, but the errors are of course bugging me with .py scripts I'm trying to run.)

Python3 packages:

macs-MacBook-Air-2% pip3 list
Package    Version
---------- -------
pip        19.2.3
psycopg2   2.8.4
setuptools 41.4.0
wheel      0.33.6

Python3 psycopg2 error:

macs-MacBook-Air-2% python3
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import psycopg2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'psycopg2'

I've only done:

pip3 install psycopg2

Extra notes:

As a side note, everything works fine when I run 2.7. (I used pip install psycopg2.)

macs-MacBook-Air-2% python
Python 2.7.10 (default, Oct  6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import psycopg2
>>> psycopg2.__version__
'2.8.4 (dt dec pq3 ext lo64)'
>>>

python 2.7 packages:

macs-MacBook-Air-2% pip list
DEPRECATION: ...2.7 end of life notice..
Package                                Version
-------------------------------------- -----------
altgraph                               0.10.2
astroid                                1.6.6
...more
psycopg2                               2.8.4
...more
zope.interface                         4.6.0

I'm very new to coding, and I searched this error. But, I did not find results that made sense to me including both a python3 error + successful python3 installation.

This could be because your default python installation is Python 2. I think you should create a virtual environment and the install psycopg2 on it. This way you will use pip3 and have isolated dependencies that won't generate conflicts with other versions (and maybe corrupt your system):

python3 -m venv ~/.environments/test
source ~/.environments/test/bin/activate
pip install psycopg2

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