我正在尝试在Flask_App所在的下载目录中执行“ flask run”。我的Flask_App是“ applications.py”。当我在开发环境中执行“烧瓶运行”时,我得到一个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,但不接受我当前的管理员密码。我在安装psycopg2时也遇到了问题,但通过重新下载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,我希望页面显示文本“ Project 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.
相同的代码也出现在我的终端窗口中。关于如何进行或如何解决此问题的任何建议?提前非常感谢您!
答案 0 :(得分:2)
我在这里有同样的错误:
导入错误: dlopen(/Users/johnndoe/dev/venv/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so, 2):未找到符号:_PQencryptPasswordCon
并且我确认执行以下命令后,问题不再存在:
31.84553546802462 = q * 37.42238795518659 + (1-q) * 26.434164513349884
感谢@Yubo
答案 1 :(得分:1)
我遇到了同样的错误,对我来说,解决方案是在虚拟环境中安装与基本Python网站包中相同版本的psycop2和psycopg2-binary
将版本设置为基本python lib:在我的情况下为2.7.7 /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/psycopg2-2.7.7.dist-info
背景详细信息: Mac Os Catalina 10.15,Python3.6.8,pipenv postgres 10。 Pycharm 2019.2.3请求在虚拟环境中安装psycopg2,尽管基本Python3.6.8中有一个副本。
我安装了最新版本:psycopg2 2.8.3并运行到该问题。我按建议尝试了psycopg2 2.6、2.6.1,但失败了。我尝试了其他一些解决方案,但失败了。
我安装了与基本Python软件包相同版本的psycopg2,问题已解决。就我而言,是2.7.7
pipenv install psycpg2==2.7.7
pipenv install psycopg2-binary==2.7.7
我不知道为什么要在虚拟环境中再次安装psycopg2。
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"
答案 2 :(得分:0)
克隆并安装Django项目后,我遇到了同样的错误。我通过卸载psycopg2
然后重新安装来解决此问题,全部都在虚拟环境中:
pip uninstall psycopg2
然后是pip install psycopg2
答案 3 :(得分:0)
在虚拟环境中,我做了以下更改:
pip uninstall psycopg2
pip install psycopg2
pip install psycopg2-binary
对我来说做得很好