0

I am running an app in apache2 with wsgi, but I am getting this error:

> mod_wsgi (pid=26904): Exception occurred processing WSGI script
> '/var/www/multidb/multidb/wsgi.py'. Traceback (most recent call last):
> File "/usr/lib/python3/dist-packages/django/core/handlers/wsgi.py",
> line 170, in call self.load_middleware() File
> "/usr/lib/python3/dist-packages/django/core/handlers/base.py", line
> 50, in load_middleware mw_class = import_string(middleware_path) File
> "/usr/lib/python3/dist-packages/django/utils/module_loading.py", line
> 26, in import_string module = import_module(module_path) File
> "/usr/lib/python3.5/importlib/init.py", line 126, in import_module
> return _bootstrap._gcd_import(name[level:], package, level) File "",
> line 986, in _gcd_import File "", line 969, in _find_and_load File "",
> line 944, in _find_and_load_unlocked File "", line 222, in
> _call_with_frames_removed File "", line 986, in _gcd_import File "", line 969, in _find_and_load File "", line 956, in
> _find_and_load_unlocked ImportError: No module named 'subdomains'

this is my middleware which contains subdomains:

MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'subdomains.middleware.SubdomainURLRoutingMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

I have installed django-subdomains using pip, and it's available in site-packages.

  • 2
    and is it in `INSTALLED_APPS `? – karthikr Jul 10 '16 at 00:05
  • yes, i tried with that but same issue. to me it looks like apache is not picking up the site-packages installed in virtualenv. i also tried with adding rest_framework app in INSTALLED_APPS and it gave the same error for that too. or it could be an issue with the python version used. my app works perfectly with python3 and i have created virtualenv using python3 and also i installed libapache2-mod-wsgi-py3 on my system but it gives this error: mod_wsgi (pid=32009): Target WSGI script '/var/www/multidb/multidb/wsgi.py' cannot be loaded as Python module. – Adarshdeep Singh Jul 10 '16 at 06:30

1 Answers1

0

When working in a virtual environment, you need to set the PYTHONPATH variable.

You can do the following:

  • Make a init.sh file just outside your project directory
  • Make the file executable (<chmod 777 filename>)
  • In the init.sh:

ABS_WORK_DIR = 'pwd -P'

export PYTHONPATH = ${ABS_WORK_DIR}/apps:${ABS_WORK_DIR}/lib:${ABS_WORK_DIR}/local

Community
  • 1
  • 1
Ankur Gupta
  • 707
  • 1
  • 9
  • 20
  • I have already added below lines in site.conf file: WSGIDaemonProcess site.com python-path=/var/www/multidb:/var/www/multidb/venv/lib/python3.5/site-packages WSGIProcessGroup site.com do i still need to add that init.sh file, if so, then how and when i need to execute that file? – Adarshdeep Singh Jul 10 '16 at 09:56
  • The above answer assumed that you are using a virtual environment... But it seems that when activating the virtualenv it sets the PYTHONPATH on its own... Are you sure you are activating the virtual environment? – Ankur Gupta Jul 10 '16 at 10:42
  • 1
    You don't set ``PYTHONPATH`` environment variable when using mod_wsgi. You should set ``python-home`` option of ``WSGIDaemonProcess`` directive to be the root of the virtual environment. See http://blog.dscpl.com.au/2014/09/using-python-virtual-environments-with.html Ensure that the directories are accessible to the user that Apache runs as. – Graham Dumpleton Jul 10 '16 at 13:51
  • i tied setting the python-home, but with that i am getting another issue. now it says `mod_wsgi (pid=27867): Target WSGI script '/var/www/multidb/multidb/wsgi.py' cannot be loaded as Python module. .... ImportError: No module named 'multidb'` looks like not able to read the application path which is /var/www/multidb – Adarshdeep Singh Jul 10 '16 at 17:29
  • for more information, I am using python3.5, libapache2-mod-wsgi-py3, django-1.9, please let me know if any other information is required. it's already more then 48 hrs now and i don't have any solution yet. – Adarshdeep Singh Jul 10 '16 at 18:31
  • What version of Apache are you using? I'd also highly recommend building `mod_wsgi` from source, as most packages provided by distros are very, very out of date. – FlipperPA Jul 11 '16 at 11:28