为啥 apache 看不到我的 python 模块?

Posted

技术标签:

【中文标题】为啥 apache 看不到我的 python 模块?【英文标题】:Why can't apache see my python module?为什么 apache 看不到我的 python 模块? 【发布时间】:2015-08-04 17:33:12 【问题描述】:

我在 Ubuntu 14.04 上使用 apache 2.4 中的 mod_wsgi 运行 Python 3.4 virtualenv。由于某种原因,apache 看不到pyodbc.cpython-34m.so 模块(当然,它缺少.py 扩展)。该模块位于我的virtualenv site-packages 目录中,扩展名为cpython-34m.so

(python3env)user@vlinuxweb:/home/production_code/python3env/lib/python3.4/site-packages$ ls
django                              pyasn1-0.1.8-py3.4.egg-info
Django-1.7.8.dist-info              __pycache__
django_mssql-1.7.dist-info          pymssql-2.1.1-py3.4.egg-info
django_pymssql-1.7.0.dist-info      pymssql.cpython-34m.so
django_pyodbc                       pyodbc-3.0.10-py3.4.egg-info
django_pyodbc-0.2.8-py3.4.egg-info  pyodbc.cpython-34m.so
easy_install.py                     reportlab
ldap3                               reportlab-3.2.0-py3.4.egg-info
ldap3-0.9.8.7.dist-info             requests
_markerlib                          requests-2.7.0.dist-info
_mssql.cpython-34m.so               setuptools
PIL                                 setuptools-15.0.dist-info
Pillow-2.8.2-py3.4.egg-info         sqlalchemy
pip                                 SQLAlchemy-1.0.4-py3.4.egg-info
pip-7.1.0.dist-info                 sqlserver_ado
pkg_resources                       sqlserver_pymssql
pyasn1

在 virtualenv 中,我可以毫无问题地导入pymssql

Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymssql
>>> pymssql.__file__
'/home/production_code/python3env/lib/python3.4/site-packages/pymssql.cpython-34m.so'

但是,在 apache 上运行的脚本无法看到该模块。

相关的apache2.conf 行:

ServerName localhost
# WSGIDaemonProcess application
WSGIPythonPath /home/production_code/python3env/lib/python3.4/site-packages:/home/production_code/school
# WSGIProcessGroup application
WSGIScriptAlias / /home/production_code/school/school/wsgi.py
# Python virtualenv home
WSGIPythonHome /home/production_code/python3env
# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf

来自 apache2 errors.log 文件的错误:

Target WSGI script '/home/production_code/school/school/wsgi.py' cannot be loaded as Python module.
Exception occurred processing WSGI script '/home/production_code/school/school/wsgi.py'.
Traceback (most recent call last):
   File "/home/production_code/school/school/wsgi.py", line 17, in <module>
     application = get_wsgi_application()
   File "/home/production_code/python3env/lib/python3.4/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
     django.setup()
   File "/home/production_code/python3env/lib/python3.4/site-packages/django/__init__.py", line 21, in setup
     apps.populate(settings.INSTALLED_APPS)
   File "/home/production_code/python3env/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate
     app_config.import_models(all_models)
   File "/home/production_code/python3env/lib/python3.4/site-packages/django/apps/config.py", line 202, in import_models
     self.models_module = import_module(models_module_name)
   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
     __import__(name)
   File "/home/production_code/python3env/lib/python3.4/site-packages/django/contrib/auth/models.py", line 40, in <module>
     class Permission(models.Model):
   File "/home/production_code/python3env/lib/python3.4/site-packages/django/db/models/base.py", line 122, in __new__
     new_class.add_to_class('_meta', Options(meta, **kwargs))
   File "/home/production_code/python3env/lib/python3.4/site-packages/django/db/models/base.py", line 297, in add_to_class
     value.contribute_to_class(cls, name)
   File "/home/production_code/python3env/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 "/home/production_code/python3env/lib/python3.4/site-packages/django/db/__init__.py", line 40, in __getattr__
     return getattr(connections[DEFAULT_DB_ALIAS], item)
   File "/home/production_code/python3env/lib/python3.4/site-packages/django/db/utils.py", line 242, in __getitem__
     backend = load_backend(db['ENGINE'])
   File "/home/production_code/python3env/lib/python3.4/site-packages/django/db/utils.py", line 108, in load_backend
     return import_module('%s.base' % backend_name)
   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
     __import__(name)
   File "/home/production_code/python3env/lib/python3.4/site-packages/django/db/backends/sqlserver_pymssql/base.py", line 5, in <module>
     import pymssql as Database
ImportError: No module named pymssql

有人知道我该如何解决这个问题吗? mod_wsgi 需要对.so 文件进行特殊配置吗?

【问题讨论】:

【参考方案1】:

试试这个:

sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi
sudo apt-get install libapache2-mod-wsgi-py3
pip3 install mod_wsgi

并检查您的虚拟主机是否已授予所有权限 这个:

<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

还要检查你项目中 mod wsgi.py 的权限

【讨论】:

是的,我相信这已经解决了。关键是要么删除libapache2-mod-python(我在其他地方读到的可能与mod-wsgi冲突)或安装libapache2-mod-wsgi-py3。谢谢!

以上是关于为啥 apache 看不到我的 python 模块?的主要内容,如果未能解决你的问题,请参考以下文章

为啥找不到我的芹菜配置文件?

为啥 PyCharm 找不到正确版本的 pip 来安装 Python 模块?

为啥我的 Laravel 模块中出现“找不到类”错误?

为啥即使安装了 FBX 模块也找不到?

python语言怎么安装xlwt,我安装好多次了已经显示成功了,为啥一运行还说找不到xlwt模块

为啥我的 Apache PHP 构建中没有启用 XML?