为什么apache看不到我的python模块?

时间:2015-08-04 17:33:13

标签: python django apache mod-wsgi pymssql

我在Ubuntu 14.04上运行Python 3.4 virtualenv,在apache 2.4中使用mod_wsgi。出于某种原因,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 个答案:

答案 0 :(得分:2)

试试这个:

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的权限