postgresql 即使在成功安装psycopg2之后,也没有名为“psycopg2”的模块错误[关闭]

yzxexxkh  于 2023-11-18  发布在  PostgreSQL
关注(0)|答案(3)|浏览(189)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受回答。

这个问题是由错字或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
7天前关闭
Improve this question
我按照here的指示,当研究如何安装psycopg2在我的新Macbook pro与M1芯片。

brew install libpq --build-from-source
brew install openssl

export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib -L/opt/homebrew/opt/libpq/lib"
export CPPFLAGS="-I/opt/homebrew/opt/[email protected]/include -I/opt/homebrew/opt/libpq/include"

pip3 install psycopg2

字符串
安装成功,这是我再次尝试运行最后一行时的输出

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: psycopg2 in /Users/yvonnetan/Library/Python/3.8/lib/python/site-packages (2.9.3)


因此,它似乎是安装成功,但当我运行我的代码,同样的错误出现:
验证码:

DATABASE_URI = f"postgresql://{credentials.user}:{credentials.pwd}@{credentials.host}:{credentials.port}/{credentials.db}"
engine = create_engine(DATABASE_URI)


错误:

ModuleNotFoundError                       Traceback (most recent call last)
/Users/yvonnetan/Documents/people-data-science/product-metrics/notebooks/product_metrics_automation.ipynb Cell 3' in <cell line: 10>()
      7 import credentials
      9 DATABASE_URI = f"postgresql://{credentials.user}:{credentials.pwd}@{credentials.host}:{credentials.port}/{credentials.db}"
---> 10 engine = create_engine(DATABASE_URI)

File <string>:2, in create_engine(url, **kwargs)

File ~/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/util/deprecations.py:309, in deprecated_params.<locals>.decorate.<locals>.warned(fn, *args, **kwargs)
    302     if m in kwargs:
    303         _warn_with_version(
    304             messages[m],
    305             versions[m],
    306             version_warnings[m],
    307             stacklevel=3,
    308         )
--> 309 return fn(*args, **kwargs)

File ~/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/engine/create.py:560, in create_engine(url, **kwargs)
    558         if k in kwargs:
    559             dbapi_args[k] = pop_kwarg(k)
--> 560     dbapi = dialect_cls.dbapi(**dbapi_args)
    562 dialect_args["dbapi"] = dbapi
    564 dialect_args.setdefault("compiler_linting", compiler.NO_LINTING)

File ~/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py:782, in PGDialect_psycopg2.dbapi(cls)
    780 @classmethod
    781 def dbapi(cls):
--> 782     import psycopg2
    784     return psycopg2

ModuleNotFoundError: No module named 'psycopg2'
engine = create_engine(DATABASE_URI)


有人能帮帮我吗?

shyt4zoc

shyt4zoc1#

你可以试着重启你的机器,对我来说这是个好办法。
如果没有,你可以试试:
(step 1仅当您正在使用应用程序时)
1.安装postgres应用程序后运行此命令并初始化服务器
sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin|sudo tee /etc/paths.d/postgresapp
1.重建您的环境

3mpgtkmj

3mpgtkmj2#

我认为你是在两个不同的Python安装之间混合。
安装psycopg2的位置为

/Users/yvonnetan/Library/Python/3.8/lib/python/site-packages (2.9.3)
                         ^^^^^^^^^^

字符串
但是在conda环境中运行代码

~/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py:782
     ^^^^^^^^^^^^^^^^^^^^^^^^


选择一个或另一个,看看你运行的是哪一个python运行which pythonwhich python3。安装软件包与conda运行:conda install psycopg2。但由于安装在python3.8上是成功的,我建议你坚持使用这一个。
阅读更多:

vof42yt1

vof42yt13#

如果你使用的是VS代码,通过提供requiremnet.txt和所有的依赖项(包括psycopg 2和psycopg 2-binary)来重新创建环境。

相关问题