为什么python3调用python2库?

时间:2016-02-01 17:17:00

标签: python django

尝试访问使用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

1 个答案:

答案 0 :(得分:1)

我在CentOS 7和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