trouble with python manage.py migrate -> No module

2019-02-25 16:23发布

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.

2条回答
【Aperson】
2楼-- · 2019-02-25 16:58

It's because you use Django in a python virtualenv and as stated on virtualenv web site :

It creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either).

That means that you need to install psycopg2 in your virtualenv and not globaly or make it access globaly installed libraries.

查看更多
该账号已被封号
3楼-- · 2019-02-25 17:14

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.

查看更多
登录 后发表回答