Why can't apache see my python module?

2019-08-30 09:30发布

问题:

I am running a Python 3.4 virtualenv on Ubuntu 14.04 with mod_wsgi in apache 2.4. For some reason, apache cannot see the pyodbc.cpython-34m.so module (which, of course, lacks a .py extension). The module is within my virtualenv site-packages directory with a cpython-34m.so extension:

(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

Within the virtualenv, I can import pymssql without any trouble at all:

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'

The script running on apache cannot see the module, however.

The relevant apache2.conf lines:

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

The error from the apache2 errors.log file:

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

Does anyone know how I might troubleshoot this? Does mod_wsgi need any special configuration for .so files?

回答1:

Try This:

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

and check in your virtual host if have Require all granted this:

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

And also check the permissions of mod wsgi.py in your project