使用 PostgreSQL (psycopg2) 和 Django 运行 Python 3.4 时出错
Error running Python 3.4 with PostgreSQL (psycopg2) and Django
因此,如果我使用 python
启动 Python 2.7 的 shell,然后运行 import psycopg2
,我不会收到任何错误。但是,如果我尝试在 Python 3.4 的 shell 中运行 import psycopg2
,我会得到:ImportError: No module named 'psycopg2'
。另一方面,导入 django 在 shells 上工作正常(在使用 pip3 安装它之后)。
其次,如果我运行 python3 manage.py runserver
,我会得到一个错误(见下文),而如果我只使用 python
,我不会。我认为这是因为我描述的第一个问题,但我不确定(如果这是一个愚蠢的问题,我很抱歉,我真的是 Django 的新手 - 试图通过教程)。
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 23, in < module >
import psycopg2 as Database
ImportError: No module named 'psycopg2'
During handling of the above exception, another exception occurred:
Traceback(most recent call last):
File "manage.py", line 10, in < module >
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level: ], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/contrib/auth/models.py", line 40, in < module >
class Permission(models.Model):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py", line 122, in __new__
new_class.add_to_class('_meta', Options(meta, * * kwargs))
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py", line 297, in add_to_class
value.contribute_to_class(cls, name)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/options.py", line 166, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/__init__.py", line 40, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/utils.py", line 242, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/utils.py", line 108, in load_backend
return import_module('%s.base' % backend_name)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level: ], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 27, in < module >
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'
我该如何解决?
此外,我对整个 Python 与 Django 的 2 对 3 业务感到非常困惑(如果这是一个非常基本的问题,再次抱歉)。运行 django-admin.py startproject theproject
时,如何指定要与 Django 一起使用的 Python 版本?是否只是在 Python 3 中编写代码并使用 python3
而不是 python
运行所有内容一样简单?
确保你有 运行 pip3 install psycopg2。
默认情况下,django-admin.py
将 运行 使用 Python 2。没关系,因为它所做的只是创建项目的文件夹布局。
创建项目后,您可以编写Python 3 代码,并且运行 您的站点使用python3 manage.py runserver
以使用Python 3.
稍后,您可能想要研究虚拟环境。如果您创建虚拟环境使用 Python 3
virtualenv v -p python3
那你可以运行
./manage.py runserver
在您的虚拟环境中,它将使用 Python 3.
如果您安装了多个 python 版本,那么安装 psycopg 的最佳解决方案是从
下载存档
http://initd.org/psycopg/download/
然后通过 运行 安装(在我的例子中,我想为 python 3.4 安装它)
python3.4 setup.py 构建
sudo python3.4 setup.py 安装
现在,输入以下内容检查它是否已安装:
python3.4
help()
modules
它应该在模块列表中显示 psycopg2。
我 运行 遇到了同样的问题,因为我在 Mac 上使用 virtualenv 从 Python 3 切换到 Python 3。显然,在尝试从 Python 3 pip install psycopg2
时,它仍在使用在使用 Python 2.7 安装期间创建的轮子。所以我不得不使用
重建轮子
sudo -H pip3 install --upgrade --force-reinstall psycopg2
因此,如果我使用 python
启动 Python 2.7 的 shell,然后运行 import psycopg2
,我不会收到任何错误。但是,如果我尝试在 Python 3.4 的 shell 中运行 import psycopg2
,我会得到:ImportError: No module named 'psycopg2'
。另一方面,导入 django 在 shells 上工作正常(在使用 pip3 安装它之后)。
其次,如果我运行 python3 manage.py runserver
,我会得到一个错误(见下文),而如果我只使用 python
,我不会。我认为这是因为我描述的第一个问题,但我不确定(如果这是一个愚蠢的问题,我很抱歉,我真的是 Django 的新手 - 试图通过教程)。
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 23, in < module >
import psycopg2 as Database
ImportError: No module named 'psycopg2'
During handling of the above exception, another exception occurred:
Traceback(most recent call last):
File "manage.py", line 10, in < module >
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level: ], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/contrib/auth/models.py", line 40, in < module >
class Permission(models.Model):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py", line 122, in __new__
new_class.add_to_class('_meta', Options(meta, * * kwargs))
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py", line 297, in add_to_class
value.contribute_to_class(cls, name)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/options.py", line 166, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/__init__.py", line 40, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/utils.py", line 242, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/utils.py", line 108, in load_backend
return import_module('%s.base' % backend_name)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level: ], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 27, in < module >
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'
我该如何解决?
此外,我对整个 Python 与 Django 的 2 对 3 业务感到非常困惑(如果这是一个非常基本的问题,再次抱歉)。运行 django-admin.py startproject theproject
时,如何指定要与 Django 一起使用的 Python 版本?是否只是在 Python 3 中编写代码并使用 python3
而不是 python
运行所有内容一样简单?
确保你有 运行 pip3 install psycopg2。
默认情况下,django-admin.py
将 运行 使用 Python 2。没关系,因为它所做的只是创建项目的文件夹布局。
创建项目后,您可以编写Python 3 代码,并且运行 您的站点使用python3 manage.py runserver
以使用Python 3.
稍后,您可能想要研究虚拟环境。如果您创建虚拟环境使用 Python 3
virtualenv v -p python3
那你可以运行
./manage.py runserver
在您的虚拟环境中,它将使用 Python 3.
如果您安装了多个 python 版本,那么安装 psycopg 的最佳解决方案是从
下载存档http://initd.org/psycopg/download/
然后通过 运行 安装(在我的例子中,我想为 python 3.4 安装它)
python3.4 setup.py 构建
sudo python3.4 setup.py 安装
现在,输入以下内容检查它是否已安装:
python3.4
help()
modules
它应该在模块列表中显示 psycopg2。
我 运行 遇到了同样的问题,因为我在 Mac 上使用 virtualenv 从 Python 3 切换到 Python 3。显然,在尝试从 Python 3 pip install psycopg2
时,它仍在使用在使用 Python 2.7 安装期间创建的轮子。所以我不得不使用
sudo -H pip3 install --upgrade --force-reinstall psycopg2