Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Import Error: Symbol not found: _PQencryptPasswordConn mean and how do I fix it?

I am trying to execute 'flask run' in the Downloads directory where my Flask_App resides. My Flask_App is 'applications.py'. When I execute 'flask run' in the development environment I am getting a URL. Once I paste the URL into Safari, I get this error.


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

I am using a MacOSX High Sierra. My PostgreSQL is version 11. My python is updated to version 3.7. And pip is upgraded and psycopg is on version 2.8.3. I have tried running 'flask run' on various directories and tried moving 'applications.py' into different libraries and directories that I am currently on. I have tried using sudo but I realise that I don't know the password. In the past I force-created another admin account when I accidentally removed admin status on my admin account...don't know if this has affected sudo or not but its not accepting my current admin password. I had an issue with installing psycopg2 as well but resolved that by re-downloading PostgreSQL 11. I have successfully installed SQLAlchemy and Flask-Session as well using pip.

I tried commenting out several lines within the code and have narrowed it down to the 'create_engine' function as just having 'os.getenv("DATABASE_URL")' runs my page as expected.

This is my code for '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"

Once I pasted the URL, I expected the page to display the text "Project 1: TODO" However instead I get this Error message:

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.

This same code appears in my terminal window as well. Any suggestions as to how to proceed or how to solve this issue? Thank you very much in advance!

like image 821
shruti iyer Avatar asked Jul 27 '19 23:07

shruti iyer


4 Answers

I ran into this same error after having cloned and installed a Django project. I solved it by uninstalling psycopg2 and then re-installing it, all within the virtual env:

pip uninstall psycopg2 and then pip install psycopg2

like image 187
Steve Perry Avatar answered Oct 20 '22 15:10

Steve Perry


I run to the same error, solution to me is that installing the same version psycop2 & psycopg2-binary in virtual env as in base Python site-packages

Set the version as in base python lib: in my case is 2.7.7 /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/psycopg2-2.7.7.dist-info

Background Details: Mac Os Catalina 10.15, Python3.6.8, pipenv postgres 10. Pycharm 2019.2.3 request psycopg2 installation on virtual env, although there is a copy in base Python3.6.8.

I install the latest version: psycopg2 2.8.3 and run to the problem. I tried psycopg2 2.6, 2.6.1 as suggested, failed. I tried a few other solutions and failed.

I install psycopg2 the same version as in base Python package, problem solved. In my case, 2.7.7

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

I do not know why install psycopg2 again in virtual env.

pipenv Pipfile as follwing:

[[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"
like image 32
Yubo Avatar answered Oct 20 '22 15:10

Yubo


I had the same error here:

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

and I confirm that after executing the following commands the issue was not there anymore :

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

Thanks @Yubo

like image 31
J2U Avatar answered Oct 20 '22 15:10

J2U


In the virtual environment, I made these changes :

pip uninstall psycopg2 
pip install psycopg2
pip install psycopg2-binary

worked very well for me

like image 24
VinuthaBS Avatar answered Oct 20 '22 16:10

VinuthaBS