postgresql 导入错误:未找到符号:_PQencryptPasswordConn是什么意思,我该如何修复它?

hof1towb  于 2023-06-05  发布在  PostgreSQL
关注(0)|答案(5)|浏览(219)

我试图在Flask_App所在的Downloads目录中执行'flask run'。我的Flask_App是'applications.py'。当我在开发环境中执行'flask run'时,我会得到一个URL。一旦我将URL粘贴到Safari,我就会得到这个错误。

ImportError: dlopen(/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so, 2): Symbol not found: _PQencryptPasswordConn
  Referenced from: /Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so
  Expected in: /usr/lib/libpq.5.6.dylib
 in /Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so

我用的是MacOSX High Sierra。我的PostgreSQL版本是11。我的Python更新到3.7版本了。pip升级了,psycopg也升级到了2.8.3版本。我尝试在不同的目录下运行'flask run',并尝试将'applications.py'移动到我当前所在的不同库和目录中。我试过使用sudo,但我意识到我不知道密码。在过去,当我不小心删除了我的管理员帐户的管理员状态时,我强制创建了另一个管理员帐户...不知道这是否影响了sudo,但它不接受我当前的管理员密码。我在安装psycopg 2时也遇到了问题,但通过重新下载PostgreSQL 11解决了这个问题。我已经使用pip成功安装了SQLAlchemy和Flask-Session。
我试着注解掉代码中的几行,并将其缩小到'create_engine'函数,因为只需'os.getenv(“DATABASE_URL”)'就可以按预期运行我的页面。
这是我的'applications.py'代码

import os

from flask import Flask, session
from flask_session import Session
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

app = Flask(__name__)

# Check for environment variable
if not os.getenv("DATABASE_URL"):
    raise RuntimeError("DATABASE_URL is not set")

# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)

# Set up database
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))

@app.route("/")
def index():
    return "Project 1: TODO"

一旦我粘贴了URL,我期望页面显示文本“项目1:TODO”然而,相反,我得到这个错误消息:

flask.cli.NoAppException: While importing "applications", an ImportError was raised:

Traceback (most recent call last):
  File "/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/flask/cli.py", line 235, in locate_app
    __import__(module_name)
  File "/Users/dhruvaiyer/Downloads/applications.py", line 20, in <module>
    engine = create_engine(os.getenv("DATABASE_URL"))
  File "/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/sqlalchemy/engine/__init__.py", line 425, in create_engine
    return strategy.create(*args, **kwargs)
  File "/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 81, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 584, in dbapi
    import psycopg2
  File "/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/psycopg2/__init__.py", line 50, in <module>
    from psycopg2._psycopg import (                     # noqa
ImportError: dlopen(/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so, 2): Symbol not found: _PQencryptPasswordConn
  Referenced from: /Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so
  Expected in: /usr/lib/libpq.5.6.dylib
 in /Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so
File "/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/flask/cli.py", line 325, in __call__
self._flush_bg_loading_exception()
File "/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/flask/cli.py", line 313, in _flush_bg_loading_exception
reraise(*exc_info)
File "/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/flask/cli.py", line 302, in _load_app
self._load_unlocked()
File "/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/flask/cli.py", line 317, in _load_unlocked
self._app = rv = self.loader()
File "/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/flask/cli.py", line 372, in load_app
app = locate_app(self, import_name, name)
File "/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/flask/cli.py", line 242, in locate_app
'\n\n{tb}'.format(name=module_name, tb=traceback.format_exc())
flask.cli.NoAppException: While importing "applications", an ImportError was raised: Traceback (most recent call last): File "/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/flask/cli.py", line 235, in locate_app __import__(module_name) File "/Users/dhruvaiyer/Downloads/applications.py", line 20, in <module> engine = create_engine(os.getenv("DATABASE_URL")) File "/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/sqlalchemy/engine/__init__.py", line 425, in create_engine return strategy.create(*args, **kwargs) File "/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 81, in create dbapi = dialect_cls.dbapi(**dbapi_args) File "/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 584, in dbapi import psycopg2 File "/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/psycopg2/__init__.py", line 50, in <module> from psycopg2._psycopg import ( # noqa ImportError: dlopen(/Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so, 2): Symbol not found: _PQencryptPasswordConn Referenced from: /Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so Expected in: /usr/lib/libpq.5.6.dylib in /Users/dhruvaiyer/anaconda3/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so
#The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.

同样的代码也出现在我的终端窗口中。对于如何进行或如何解决这个问题有什么建议吗?非常感谢你提前!

csbfibhn

csbfibhn1#

我在克隆并安装Django项目后遇到了同样的错误。我通过卸载psycopg2然后重新安装它来解决这个问题,所有这些都在虚拟环境中进行:
pip uninstall psycopg2然后是pip install psycopg2

wgeznvg7

wgeznvg72#

我运行到相同的错误,解决方案是在虚拟环境中安装与基本Python站点包相同的版本psycop 2 & psycopg 2-binary
在base python lib中设置版本:在我例子中是2.7.7 /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/psycopg 2 -2.7.7.dist-info
背景资料:Mac OS Catalina 10.15,Python3.6.8,pipenv postgres 10. Pycharm 2019.2.3请求在虚拟环境中安装psycopg 2,尽管在基础Python3.6.8中有一个副本。
安装最新版本:psycopg 2 2.8.3并运行到问题。我按照建议尝试了psycopg 2 2.6,2.6.1,失败了。我尝试了几个其他的解决方案,但都失败了。
我安装psycopg 2的版本与基本Python包中的版本相同,问题解决了。我的情况是2.7.7

pipenv install psycpg2==2.7.7
pipenv install psycopg2-binary==2.7.7

我不知道为什么在虚拟环境中再次安装psycopg 2。
pipenv Pipfile如下:

[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[packages]
flask = "==1.0"
flask-sqlalchemy = "==2.3.2"
flask-wtf = "==0.14.2"
cymysql = "==0.9.1"
flask-cors = "==2.1.0"
flask-httpauth = "==2.7.0"
requests = "==2.18.4"
marshmallow = "*"
flask-marshmallow = "*"
marshmallow-sqlalchemy = "*"
connexion = {extras = ["swagger-ui"],version = "*"}
psycopg2 = "==2.7.7"
psycopg2-binary = "==2.7.7"

[dev-packages]

[requires]
python_version = "3.6"
qgelzfjb

qgelzfjb3#

我在这里犯了同样的错误:

ImportError:dlopen(/Users/johnndoe/dev/venv/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so,2):Symbol not found:_PQencryptPasswordCon

我确认在执行以下命令后,问题不再存在:

pip uninstall psycopg2 
pip install psycopg2==2.7.7  
pip install psycopg2-binary==2.7.7

谢谢@Yubo

cetgtptt

cetgtptt4#

在虚拟环境中,我做了这些改变:

pip uninstall psycopg2 
pip install psycopg2
pip install psycopg2-binary

对我来说效果很好

8xiog9wr

8xiog9wr5#

在我的例子中,问题是我的_psycopg.cpython-310-darwin.so文件(像你的.so文件,但不同的版本)使用了错误的库文件,也就是错误的.dylib文件。
更准确地说,/usr/lib/libpq.dylib是一个指向libpq.5.6.dylib的符号链接,但我的项目需要一个更新版本的libpq
由于MacOS中的SIP安全保护系统,我无法更改链接/usr/lib/libpq.dylib或删除它并将其替换为另一个链接。
但是由于我使用的是venv,所以我在项目文件夹中有一个.so的本地版本。因此,我通过在MacOS中使用内置的install_name_tool来告诉.so文件应该使用正确的.dylib文件来解决这个问题。
我通过运行下面的命令实现了这一点:

install_name_tool -change '/usr/lib/libpq.5.dylib' '/usr/local/Cellar/postgresql@14/14.8/lib/postgresql@14/libpq.5.14.dylib' '/Users/andrea/myproject/venv/lib/python3.10/site-packages/psycopg2/_psycopg.cpython-310-darwin.so'

**含义:*在第三个参数给出的.so文件中, 将 * 每个对/usr/lib/libpq.5.dylib的引用修改为对/usr/local/Cellar/[...]/libpq.5.14.dylib的引用

希望这能帮上忙。

相关问题