为什么python3调用python2库? [英] Why is python3 calling python2 libraries?

查看:114
本文介绍了为什么python3调用python2库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  mod_wsgi(pid = 2603):目标WSGI脚本'/var/www/iMARS/iMARS/wsgi.py'无法作为Python模块加载.mod_wsgi(pid = 2603):处理WSGI脚本'/var/www/iMARS/iMARS/wsgi.py'时发生异常,引用:追溯(最近一次呼叫过去):,引荐来源:在< module>中的文件"/var/www/iMARS/iMARS/wsgi.py"的第16行中,引用:application = get_wsgi_application(),引荐网址:http://mana.smcnus.org/get_wsgi_application中第13行的文件"/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/core/wsgi.py",引用:django.setup(),引荐来源:在安装程序的第18行,文件"/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/__init__.py",引用:apps.populate(settings.INSTALLED_APPS),引荐来源:文件"/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/apps/registry.py",第108行,填充,引用如下:app_config.import_models(all_models),引荐来源:在import_models中的第202行,文件"/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/apps/config.py",引用:self.models_module = import_module(models_module_name),引荐来源:在import_module中,文件"/usr/lib64/python2.7/importlib/__init__.py",第37行,引用者:__import __(名称),引荐来源:在< module>中,文件"/var/www/iMARS/core/models.py",第2行,引用:从django.contrib.auth.models导入AbstractBaseUser,引用者:< module>中第4行的文件"/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/contrib/auth/models.py",引用:从django.contrib.auth.base_user导入AbstractBaseUser,BaseUserManager,引荐来源网址:在< module>中的文件"/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/contr ib/auth/base_user.py"中,第49行,引用:类AbstractBaseUser(models.Model):,引荐来源:__new__中的文件"/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/db/models/base.py",第108行,引用为:new_class.add_to_class('_ meta',Options(meta,app_label)),引用者:在add_to_class的第299行中,文件"/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/db/models/base.py"引用:value.contribute_to_class(cls,name),引用者:文件"/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/db/models/options.py",第263行,在tribution_to_class中,引用为:self.db_table = truncate_name(self.db_table,connection.ops.max_name_length()),引荐来源网址:__getattr__中第36行的文件"/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/db/__init__.py",引用:返回getattr(连接[DEFAULT_DB_ALIAS],项目),引用:__getitem__中的文件"/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/db/utils.py"第212行,引用:backend = load_backend(db ['ENGINE']),引荐来源:在load_backend中的文件"/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/db/utils.py",行116,引用:返回import_module('%s.base'%backend_name),引荐来源:在import_module中,文件"/usr/lib64/python2.7/importlib/__init__.py",第37行,引用者:__import __(名称),引荐来源:文件"/opt/virtualenvs/iMARS/lib/python3.4/site-packages/django/db/backends/postgresql/base.py",第24行,在< module>中,引用:引发ImproperlyConfigured(加载psycopg2模块时出错:%s"%e),引用程序:配置不正确:加载psycopg2模块时出错:没有名为_psycopg的模块 

解决方案

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

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

 源/opt/virtualenvs/iMARS/bin/activatepython manage.py shell 

如果可行,则问题出在mod_wsgi上(在CentOS上),您可以像这样进行更新:

  sudo yum清除mod_wsgi须藤百胜安装python34u-mod_wsgisudo apachectl重新启动 

此问题为Ubuntu提供了解决方案: apache2上的Django MySQLdb问题

The following error is generated from apache2 when trying to access a website created using django. as you can see from the error below, psycopg2 has failed to load. This is probably caused by the fact that it is trying to use python2 libraries instead of python3. So I am wondering how did this happen and what is the fix for it. Thanks

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

解决方案

I got a similar issue with mysqlclient on CentOS 7 and apache where the default mod_wsgi is for python 2.

First try to check if you can run your Django application from the shell:

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

If this works, the problem is with mod_wsgi which (on CentOS) you can update like this:

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

This question provides the solution for Ubuntu: Django MySQLdb issue on apache2

这篇关于为什么python3调用python2库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆