Django ·波斯特格瑞斯心理学2:即使安装了模块,配置也不正确

c7rzv4ha  于 2022-11-18  发布在  Go
关注(0)|答案(1)|浏览(145)

我是第一次使用Django,但之前使用过PostgreSQL。我尝试按照official Django tutorial来设置数据库。我已经按照所有步骤进行了设置,但在使用命令“python manage.py migrate”时出现错误,即使我有psycopg2,也找不到它

installed.Traceback (most recent call last):
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/db/backends/postgresql/base.py", line 24, in <module>
    import psycopg2 as Database
ModuleNotFoundError: No module named 'psycopg2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/alexanderverheecke/Documents/GitHub/mysite/manage.py", line 22, in <module>
    main()
  File "/Users/alexanderverheecke/Documents/GitHub/mysite/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/core/management/__init__.py", line 420, in execute
    django.setup()
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/apps/registry.py", line 116, in populate
    app_config.import_models()
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/apps/config.py", line 269, in import_models
    self.models_module = import_module(models_module_name)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/db/models/base.py", line 141, in __new__
    new_class.add_to_class("_meta", Options(meta, app_label))
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/db/models/base.py", line 369, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/db/models/options.py", line 231, in contribute_to_class
    self.db_table, connection.ops.max_name_length()
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/utils/connection.py", line 15, in __getattr__
    return getattr(self._connections[self._alias], item)
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/utils/connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/db/utils.py", line 193, in create_connection
    backend = load_backend(db["ENGINE"])
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/db/utils.py", line 113, in load_backend
    return import_module("%s.base" % backend_name)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/alexanderverheecke/Library/Python/3.9/lib/python/site-packages/django/db/backends/postgresql/base.py", line 28, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'

我正在使用Django教程中提到的设置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'DatabaseName',
        'USER': 'UserName',
        'PASSWORD': 'Userpassword',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

我已确认已在同一位置安装了必需的软件包:

pip freeze:

Django==4.1.3
psycopg2==2.9.5
psycopg2-binary==2.9.5

pip show Django/psycopg2/psycopg2-binary:

Django : Location: /opt/homebrew/lib/python3.10/site-packages
Psycopg2: Location: /opt/homebrew/lib/python3.10/site-packages
psycopg2-binary: Location: /opt/homebrew/lib/python3.10/site-packages
zzwlnbp8

zzwlnbp81#

看起来你使用系统python来运行你的迁移。错误追溯包含python二进制文件的以下路径:"/Users/alexanderverheecke/Library/Python/3.9/...",但是在pip show命令中,python路径是"/opt/homebrew/lib/python3.10/"
实际上我不明白这怎么可能,因为第一个看起来像Windows路径,第二个像Linux...
无论如何,请尝试以下操作,然后再次运行migrate命令:

  • 激活/停用虚拟环境(如果使用)
  • 将操作系统更改为正确的操作系统
  • 再次运行pip安装

使舒尔命令which python(适用于Linux)、gcm python(适用于Windows)返回与pip show相同的python路径。

相关问题