Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

django - trouble with python manage.py migrate -> No module named psycopg2

I am having some trouble with migrating Django using postgresql.

This is my first time with Django, and I am just following the tutorial.

As suggested on the Django website, I have created a virtualenv to run the Django project.

Next, I created a postgresql database with these settings:

enter image description here

In settings.py I have set these values for the database:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'django_tutorial',
        'USER': 'johan',
        'PASSWORD': '1234',
    }
}

When installing psycopg2 with apt-get I get this message:

(venv)johan@johan-pc:~/sdp/django_tutorial/venv/django_tutorial$ sudo apt-get install python-psycopg2
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python-psycopg2 is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 95 not upgraded.

As far as I can tell this would mean that psycopg2 is installed.

When running

$python manage.py migrate

I get the following error message:

django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named psycopg2

If needed for the answer I could provide the entire stack trace.

Could someone explain what I could do to solve this? I have also looked on Google for a solution with no luck.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

It must be because you are installing psycopg2 in your system level python installation not in your virtualenv.

sudo apt-get install python-psycopg2

will install it in your system level python installation.

You can install it in your virtualenv by

pip install psycopg2

after activating your virtualenv or you can create your virtualenv with --system-site-packages flag so that your virtualenv will have packages in your system level python already available.

virtualenv --system-site-packages test

where test is your virtualenv.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...