Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing python modules with mod_wsgi

We are trying to get out wsgi application working on mod_wsgi. We have run it before using wsgiref.simple_server, as a debugging environment. Now, we want to move it over to using Apache httpd to integrate with loading static files under the same port, and to provide an environment more suitable for production.

The httpd.conf file we have set up looks like this:

<VirtualHost *:80>

    DocumentRoot /var/www/

    ErrorLog /var/www/log/error_log

    LogLevel debug

    WSGIScriptAlias /app /var/www/python/resource/rest.py

    <Directory /var/www/python/>
            Order allow,deny
            Allow from all
    </Directory>

    <Directory />
            Order allow,deny
            Allow from all

            RewriteEngine On
            # Some rewrites go here

    </Directory>

</VirtualHost>

With this setup, we can access the main application file, rest.py (under http://myhost/app), but any imports to modules (like from module1 import function where module1 is under the same directory as rest.py) are failing with an ImportError.

I suspect this has to do with having to set some sort of environment variable, but I have added /var/www/python to sys.path on the main application method and it doesn't work. Any help or pointers with this would be really appreciated.

Thanks!

like image 205
Juan Carlos Coto Avatar asked Jan 28 '26 09:01

Juan Carlos Coto


1 Answers

Is rest.py under /var/www/python or /var/www/python/resource? Adding this directory to sys.path should work as far as I know. Preferable to using a string constant would be the following:

sys.path.append(os.path.dirname(__file__))

This should work even if you decide to deploy to a different location in the future.

like image 125
randomhuman Avatar answered Jan 30 '26 20:01

randomhuman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!