3

Guys I stuck this problem for couple weeks already. I have tried almost every answer I could find in stack overflow.

The error is below:

(venv1) Caimings-MacBook-Pro:yang NLStom$ python manage.py migrate
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/NLStom/Desktop/hey/yang/venv1/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/Users/NLStom/Desktop/hey/yang/venv1/lib/python2.7/site-packages/django/core/management/__init__.py", line 341, in execute
    django.setup()
  File "/Users/NLStom/Desktop/hey/yang/venv1/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/NLStom/Desktop/hey/yang/venv1/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/NLStom/Desktop/hey/yang/venv1/lib/python2.7/site-packages/django/apps/config.py", line 199, in import_models
    self.models_module = import_module(models_module_name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/NLStom/Desktop/hey/yang/venv1/lib/python2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Users/NLStom/Desktop/hey/yang/venv1/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 52, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/NLStom/Desktop/hey/yang/venv1/lib/python2.7/site-packages/django/db/models/base.py", line 119, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Users/NLStom/Desktop/hey/yang/venv1/lib/python2.7/site-packages/django/db/models/base.py", line 316, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/NLStom/Desktop/hey/yang/venv1/lib/python2.7/site-packages/django/db/models/options.py", line 214, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/NLStom/Desktop/hey/yang/venv1/lib/python2.7/site-packages/django/db/__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/NLStom/Desktop/hey/yang/venv1/lib/python2.7/site-packages/django/db/utils.py", line 211, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/NLStom/Desktop/hey/yang/venv1/lib/python2.7/site-packages/django/db/utils.py", line 115, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/NLStom/Desktop/hey/yang/venv1/lib/python2.7/site-packages/django/db/backends/postgresql/base.py", line 24, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: dlopen(/Users/NLStom/Desktop/hey/yang/venv1/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Symbol not found: _PQbackendPID
  Referenced from: /Users/NLStom/Desktop/hey/yang/venv1/lib/python2.7/site-packages/psycopg2/_psycopg.so
  Expected in: dynamic lookup

I tried to using PostgreSql for my database in Django. Below is code in my setting

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'play',
        'USER': 'uaa',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '',
    }
}

What have I did in PostgreSql.

psql (9.5.4)
Type "help" for help.

NLStom=# CREATE uaa;
ERROR:  syntax error at or near "uaa"
LINE 1: CREATE uaa;
               ^
NLStom=# CREATE USER uaa;
CREATE ROLE
NLStom=# CREATE DATABASE play OWNER uaa;
CREATE DATABASE
NLStom=# \q

I used pip brew to install PostgreSql and used pip to install psycopg2. I have done all of these above in virtualenv.

Also wanna mention that I install Postgres.app in my mac.

NLStom
  • 41
  • 5

2 Answers2

0

I got a similar error, but once I verified that psql was in my PATH and the postgres server was running in the background, I reinstalled psycopg2 with conda install psycopg2, and my django postgres app worked fine.

hobs
  • 18,473
  • 10
  • 83
  • 106
0

Got similar issue on macOS 11.01, M1 Chip. Solved it by deleting the venv created by pycharm and recreating it manually with python3 -m venv /path/to/venv and then pip3 install -r requirements.txt. Following github issue could be helpful.

anand
  • 369
  • 2
  • 8