简体   繁体   English

ModuleNotFoundError: No module named 'psycopg2' (python3, mac, pip3 显示 psycopg2 已成功安装)

[英]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.问题:我在 python3 中收到了 psycopg2 的 ModuleNotFoundError,尽管它已通过 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.) (我发布了简短的代码来从终端进行总结,但错误当然是我试图运行的 .py 脚本。)

Python3 packages: Python3 包:

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: Python3 psycopg2 错误:

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.作为旁注,当我运行 2.7 时一切正常。 (I used pip install psycopg2.) (我使用了 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: python 2.7 包:

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.但是,我没有找到对我有意义的结果,包括 python3 错误 + 成功安装 python3。

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.这可能是因为您的默认 python 安装是 Python 2。我认为您应该创建一个虚拟环境并在其上安装 psycopg2。 This way you will use pip3 and have isolated dependencies that won't generate conflicts with other versions (and maybe corrupt your system):这样,您将使用 pip3 并具有独立的依赖项,不会与其他版本产生冲突(并且可能会损坏您的系统):

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

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

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