Skip to content Skip to sidebar Skip to footer

Apache + Mod_wsgi - Python Doesn't Load Installed Modules

I have an Apache server with mod_wsgi, running an Python 2.7 script. The script uses the python Pillow module, installed via pip. Running the script normally using python script.p

Solution 1:

in my case, the modules were installed in a user env, vs the machine's env. i just run:

sudo -H pip3.7 install mako

The -H says to sudo to install the module in the root machine directory vs user directory who run the command ...

this is because Apache cannot access/read personal user's files.

Solution 2:

I redid the server configuration, this time naming things properly, used virtualenv, and Daemon mode to wsgi.

Here's the apache configuration I ended up with:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        WSGIDaemonProcess sprites-toolbox python-path=/home/nitay/Desktop/SpritesToolbox/Python:/home/nitay/Desktop/SpritesToolbox/Python/sprite-toolbox-env/lib/python2.7/site-packages
        WSGIProcessGroup sprites-toolbox

        WSGIScriptAlias /wsgi/ /home/nitay/Desktop/SpritesToolbox/Python/wsgi.py


        <Directory "/home/nitay/Desktop/SpritesToolbox/Python">
            Require all granted
        </Directory>
</VirtualHost>

Moral of the story? "Time is precious, waste it wisely" (Don't half-ass server configurations)

Solution 3:

I got stuck with this and the discussion above really helped. My solution however, was simply to set up the Python path at the beginning of my WSGI program, e.g.:

def application(environ, start_response):
    import sys
    path = "/usr/local/lib64/python2.7/site-packages/"ifpathnotin sys.path: sys.path.append(path)

Post a Comment for "Apache + Mod_wsgi - Python Doesn't Load Installed Modules"