WSGI: ImportError: No module named hello (module in the same directory of the main .py file)
Asked Answered
T

2

0

This is not a duplicate of Apache with virtualenv and mod_wsgi : ImportError : No module named 'django' since here I'm not using any virtualenv, and also I'm not trying to import another framework's module (such as django), but just a module in the same directory.


Here is my setup:

/var/www/test/app.py:

import os, time, sys
from bottle import route, run, template, default_app

os.chdir(os.path.dirname(os.path.abspath(__file__)))

import hello

@route('/')
def index():
    return 'Hello world Python ' + sys.version

application = default_app()

/var/www/test/hello.py:

# just an example module
def test():
    print 'hello'

Apache config:

<VirtualHost *:80>
  ServerName example.com
  <Directory />
    Require all granted
  </Directory>
  WSGIScriptAlias / /var/www/test/app.py
  WSGIDaemonProcess test user=www-data group=www-data processes=5 threads=5 display-name=test python-path=/var/www/test/
</VirtualHost>

Then I get:

ImportError: No module named hello

What is incorrect? Shouldn't WSGIDaemonProcess ... python-path=/var/www/test/ help the module hello to be loaded?

Tenney answered 28/11, 2019 at 11:37 Comment(0)
T
0

The solution is:

  • to have a WSGIDaemonProcess ... python-path=... indeed,

  • but also a WSGIScriptAlias ... process-group=... (not sure why this process-group parameter is linked to allowing or not to load modules from the same directory, but it works!)

Example:

WSGIScriptAlias / /var/www/test/app.py process-group=test
WSGIDaemonProcess test user=www-data group=www-data processes=5 threads=5 display-name=test python-path=/var/www/test/

See also: https://bottlepy.org/docs/dev/deployment.html#apache-mod-wsgi

Tenney answered 28/11, 2019 at 12:45 Comment(0)
Z
1

Apache does not change to the current working directory, thus it will not search for hello where you think.

You might change app.py in following way.

import os, time, sys
from bottle import route, run, template, default_ap
# add the scripts directory to the python path so that hello can be found
sys.path.insert(0, os.path.realpath(os.path.dirname(__file__)))

Advantage you do not have to mess with the apache config files

Zaffer answered 28/11, 2019 at 12:54 Comment(6)
Thank you for your answer. But I already did this in app.py: os.chdir(os.path.dirname(os.path.abspath(__file__))), why is this not enough?Tenney
changing the directory from within python is not working. It just changes the working directory, but no more the pythonpath. You have to change the pythonpath (sys.path)Zaffer
Oh ok, PYTHONPATH is different, you're right.Tenney
sys.path is the variable within python, that combines the contents of the environment variable PYTHONPATH and some other autodetermined values So sys.path.insert(0, "path_to_add") shall do the jobZaffer
Thanks! But still, look at my initial config in the question, I already have appended this to the WSGI configuration: python-path=/var/www/test/, why isn't it working with that? It's stange, don't you think so? The parameter WSGIDaemonProcess ... python-path=... should be exactly for this, but here it seems it's not taken in consideration if no WSGIScriptAlias ... process-group=... is given...Tenney
I don't use apache, can't help there. but try my code. Perhaps others can explain what's wrong with the apache configZaffer
T
0

The solution is:

  • to have a WSGIDaemonProcess ... python-path=... indeed,

  • but also a WSGIScriptAlias ... process-group=... (not sure why this process-group parameter is linked to allowing or not to load modules from the same directory, but it works!)

Example:

WSGIScriptAlias / /var/www/test/app.py process-group=test
WSGIDaemonProcess test user=www-data group=www-data processes=5 threads=5 display-name=test python-path=/var/www/test/

See also: https://bottlepy.org/docs/dev/deployment.html#apache-mod-wsgi

Tenney answered 28/11, 2019 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.