gpt4 book ai didi

python - 为什么 python3 调用 python2 库?

转载 作者:太空宇宙 更新时间:2023-11-04 05:37:35 24 4
gpt4 key购买 nike

当尝试访问使用 django 创建的网站时,apache2 会生成以下错误。从下面的错误中可以看出,psycopg2 加载失败。这可能是因为它试图使用 python2 库而不是 python3。所以我想知道这是怎么发生的以及解决方法是什么。谢谢

mod_wsgi (pid=2603): Target WSGI script '/var/www/iMARS/iMARS/wsgi.py' cannot be loaded as Python module., referer: 
mod_wsgi (pid=2603): Exception occurred processing WSGI script '/var/www/iMARS/iMARS/wsgi.py'., referer:
Traceback (most recent call last):, referer:
File "/var/www/iMARS/iMARS/wsgi.py", line 16, in <module>, referer:
application = get_wsgi_application(), referer: http://mana.smcnus.org/
File "/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application, referer:
django.setup(), referer:
File "/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/__init__.py", line 18, in setup, referer:
apps.populate(settings.INSTALLED_APPS), referer:
File "/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate, referer:
app_config.import_models(all_models), referer:
File "/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/apps/config.py", line 202, in import_models, referer:
self.models_module = import_module(models_module_name), referer:
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module, referer:
__import__(name), referer:
File "/var/www/iMARS/core/models.py", line 2, in <module>, referer:
from django.contrib.auth.models import AbstractBaseUser, referer:
File "/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/contrib/auth/models.py", line 4, in <module>, referer:
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager, referer:
File "/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/contr ib/auth/base_user.py", line 49, in <module>, referer:
class AbstractBaseUser(models.Model):, referer:
File "/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/db/models/base.py", line 108, in __new__, referer:
new_class.add_to_class('_meta', Options(meta, app_label)), referer:
File "/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/db/models/base.py", line 299, in add_to_class, referer:
value.contribute_to_class(cls, name), referer:
File "/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/db/models/options.py", line 263, in contribute_to_class, referer:
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()), referer:
File "/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/db/__init__.py", line 36, in __getattr__, referer:
return getattr(connections[DEFAULT_DB_ALIAS], item), referer:
File "/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/db/utils.py", line 212, in __getitem__, referer:
backend = load_backend(db['ENGINE']), referer:
File "/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/db/utils.py", line 116, in load_backend, referer:
return import_module('%s.base' % backend_name), referer:
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module, referer:
__import__(name), referer:
File "/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 24, in <module>, referer:
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e), referer:
ImproperlyConfigured: Error loading psycopg2 module: No module named _psycopg

最佳答案

我在 CentOS 7 上的 mysqlclient 和 apache 上遇到了类似的问题,其中默认的 mod_wsgi 用于 python 2。

首先尝试检查是否可以从 shell 运行 Django 应用程序:

source /opt/virtualenvs/iMARS/bin/activate
python manage.py shell

如果这有效,问题出在 mod_wsgi 上,您可以像这样更新它(在 CentOS 上):

sudo yum erase mod_wsgi
sudo yum install python34u-mod_wsgi
sudo apachectl restart

这个问题提供了Ubuntu的解决方案:Django MySQLdb issue on apache2

关于python - 为什么 python3 调用 python2 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35136437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com