添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

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
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.