Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

python - Connecting Django to PostgreSQL - Error loading psycopg2 module despite install

Very aware there are similar questions out there. I have had a sift through answers but nothing seems to be working... I'll explain from the beginning so hopefully someone can help. I am using python 3.8 on mac.

As title says, trying to connect a new django project to a postgresql database. I have installed the postgresql app (as well as pgadmin 4 for monitoring). I've updated my settings.py file as follows:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'psql1',
    'USER': 'Pete',
    'PASSWORD': '',
    'HOST': 'localhost',
    'PORT': '5432',
    }
}

I've then gone into terminal to run python manage.py migrate to make the initial migrations, but receive the following: Error loading psycopg2 module: No module named 'psycopg2'.

Obviously, I then followed the instructions online and used pip install psycopg2-binary, successfully installing psycopg2-binary in:

/Users/Pete/Library/Python/2.7/lib/python/site-packages (2.8.6)

and also pip3 install psycopg2-binary, successfully installing psycopg2-binary in:

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (2.8.6)

But I still receive the SAME error above.

Really quite stuck and would greatly appreciate any help!

Thanks.

question from:https://stackoverflow.com/questions/65910653/connecting-django-to-postgresql-error-loading-psycopg2-module-despite-install

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So, after a couple hours of more searching, I finally came across an answer that worked. However, I am stumped as to why this worked. Would someone be able to explain?

The process that worked:

  • activate pipenv
  • run command PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH"
  • install psycopg2-binary AGAIN
  • makemigrations

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...