How to get mod_wsgi to pick up my virtualenv
Asked Answered
W

5

21

I'm very new to working with Flask-

according to http://flask.pocoo.org/docs/0.12/deploying/mod_wsgi/

under the heading "Working with Virtual Environments" I read:

For Python 3 add the following lines to the top of your .wsgi file:

activate_this = '/path/to/env/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this)) This sets up the load paths according to the settings of the virtual environment.

Keep in mind that the path has to be absolute.

to activate my venv I use the command from linux:

 my_env/bin/activate

I looked in my my_env/bin/ directory and do not see any .py files. Am I suppose to create a .py file that in my_env/bin/ that will be called by the .wsgi file?

Wolof answered 8/3, 2017 at 2:14 Comment(0)
L
15

I was having the same issue, the solution is actually quite simple. You need to install libapache2-mod-wsgi-py3 instead of libapache2-mod-wsgi. The latter is for python 2.

You can then activate your environment by adding the environment's site-packages to the system path. For example, for me (using venv) I can do this by adding the following line to my *.wgsi file.

sys.path.insert(0,"/path/to/venv/lib/python3.8/site-packages")
Leman answered 30/5, 2020 at 2:46 Comment(0)
I
8

If you are using mod_wsgi, read the documentation at:

TLDR:

From Documentation - to use a Python virtual environment, all you need to do is add the python-home option to the WSGIDaemonProcess directive resulting in

add this line to your virtual host to enable virtualenv

WSGIDaemonProcess application_name python-home=/path/to/app/venv

Idolah answered 8/3, 2017 at 2:42 Comment(20)
Thanks Graham- after a couple of hours of focused reading the docs along with three shots of espresso, confirming that sites .conf and project .wsgi are correct, I may found the issue- the apache2/error.log reports "Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.12 configured " i'm using python3. From within my venv, I pip installed mod_wsgi but get exception: 'missing Apache httpd server packages.' % APXS) RuntimeError: The 'apxs' command appears not to be installed or is not executable.Wolof
You need to install the 'dev' package for Apache. See system requirements in pypi.python.org/pypi/mod_wsgiIdolah
ok as per the doc, installed apache2-dev and checked it was working using mod_wsgi-express start-server with my python 3 venv-Wolof
I see the whiskey bottle... powerful suggestion to start drinking ;-) after service apache2 restart, I still see "Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.12 configured" on the tail of the log file.Wolof
You need to uninstall any system package for mod_wsgi and after having done pip install mod_wsgi do the steps mentioned in section 'Connecting into Apache installation' of pypi.python.org/pypi/mod_wsgi Have you done that?Idolah
pip could not find any instance of mod_wsgi in system packages- as per the section you mention: (my_3env) root@pcbevo:/var/www/pcbevo/pcbevo# mod_wsgi-express module-config LoadModule wsgi_module "/var/www/pcbevo/pcbevo/my_3env/lib/python3.5/site-packages/mod_wsgi/server/mod_wsgi-py35.cpython-35m-x86_64-linux-gnu.so" WSGIPythonHome "/var/www/pcbevo/pcbevo/my_3env" (my_3env) root@pcbevo:/var/www/pcbevo/pcbevo# mod_wsgi-express install-module LoadModule wsgi_module "/usr/lib/apache2/modules/mod_wsgi-py35.cpython-35m-x86_64-linux-gnu.so" WSGIPythonHome "/var/www/pcbevo/pcbevo/my_3env"Wolof
error log tail still reports. Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.12 configuredWolof
other snips from error.log tail include- mod_wsgi (pid=8341): Target WSGI script '/var/www/pcbevo/pcbevo.wsgi' cannot be loaded as Python module. mod_wsgi (pid=8341): Exception occurred processing WSGI script '/var/www/pcbevo/pcbevo.wsgi'.Wolof
To uninstall system packages on an Ubuntu system you use apt-get. See howtoinstall.co/en/ubuntu/trusty/…Idolah
Let us continue this discussion in chat.Wolof
Please use the mod_wsgi mailing list if after research you can't work things out. That is what the list is for. SO is not a good place to have any sort of discussion. modwsgi.readthedocs.io/en/develop/finding-help.htmlIdolah
@GrahamDumpleton I read that entire page along with blog.dscpl.com.au/2014/09/…. Should the value of home in WSGIDaemonProcess example python-home=/path/to/venv home=/path/to/mysite.com be ... the same as the value for DocumentRoot? This is where the main .wsgi file is located, but most of the Python files are located 1 or 2 sub-directories below it. To make sure that all of the modules are loaded properly under mod_wsgi (using Python 3.4.x) I wonder if I need to specify every dir involved somewhere in Apache conf, or only the top level.Heliometer
You should never place any of the Python code for your site under DocumentRoot as configure your Apache wrongly and people could download your code. So have your project separate from DocumentRoot.Idolah
The home option to WSGIDaemonProcess is usually not required. It would only be necessary if your code doesn't follow best practices and is using relative paths to open files, rather than calculating absolute paths in some way. See modwsgi.readthedocs.io/en/develop/user-guides/…Idolah
The python-home value if using a Python virtual environment, should be what sys.prefix is set to when querying that from Python command line interpreter running against the virtual environment.Idolah
Instead of home, what you would normally set is python-path. This is used to add additional directories to the Python module search path. Ie., same as setting PYTHONPATH environment variable, or sys,path from code. If your project code is correctly set up as a package, you only need to list the parent directory of your package. That is, the directory above the directory containing the __init__.py for the top of your project package.Idolah
@GrahamDumpleton, thank you. Well, some of my Python scripts do read local setting(s) file(s), so it sounds like home is a useful setting. My other question is (I am not fully clear on this) - are python-path, PYTHONPATH and sys.path equivalent? It is possible that there is a precedence if more than one way is used, and there is also a question of timing. For example, is it too late to mess with sys.path at the time the .wsgi is being run? This is primarily in the context of making relative imports work. Could I just pre-pend an empty string to sys.path inside .wsgi? Thanks again.Heliometer
Even if they read local settings file, a well designed application would not use a relative path, but calculate the absolute path based on some anchor, like os.path.dirname(__file__) from code location. This is to avoid problems if any code decides to change the working directory. Similarly, using an empty string in sys.path is also bad, as it means look up relative to current directory, but if current directory changes, that will break.Idolah
For Apache, relying in `PYTHONPATH`` is usually a bad idea as you can't easily set it if a system Apache setup. You can update sys.path as long as done before non stdlib imports in WSGI script file. It is better to avoid change sys.path in WSGI script file though unless have good need. Use the settings in Apache/mod_wsgi configuration instead.Idolah
Removing the activate_this related code and adding python-home with WSGIDeamonProcess helped me move ahead.Drumbeat
D
4

The best and cleanest way I've found without doing some “kind of magic” with obscure scripts is to simply begin the .wsgi with the reference to the python interpreter that lies within the environment. Just start your .wsgi with this, and no need to fiddle after that:

#!/path/to/your/venv/bin/python

I wish I thought about this straightforward solution before unsuccessfully spending hours on this - and wish someone else had mentioned it.

Dagley answered 30/11, 2021 at 20:30 Comment(0)
B
1

Setting python-home and python-path and/or setting paths directly in wsgi.py didn't work for me . My venv (on Ubuntu 20) was using Python3.11, but I saw in the error log of Apache that it was still running the default Python 3.8 installation of my server, although it seemed to take the libraries in the right folder.

I had to install mod_wsgi library in the Python venv (not Apache !), via pip, and use it to generate and copy the config lines, containing the right paths inside of the venv, to the environment of the main Apache server:

  1. remove the default mod_wsgi module of the Apache distribution: sudo a2dismod wsgi
  2. Install apache2-dev : apt-get install apache2-dev
  3. Install mod_wsgi from within your virtualenv : pip install mod_wsgi
  4. Run, still from the venv, the following command mod_wsgi-express module-config
  5. Copy paste the output (absolute path to the local module) to apache2.conf,

e.g.:

LoadModule wsgi_module "/my_path_to_venv/lib/python3.11/site-packages/mod_wsgi/server/mod_wsgi-py311.cpython-311-x86_64-linux-gnu.so"
WSGIPythonHome "/my_path_to_venv"
Balkh answered 4/5, 2024 at 18:48 Comment(0)
B
0

Ensure that you install mod_wsgi in your virtual environment. Then, issuing the mod_wsgi-express module-config will give you the correct Apache configuration. Finally, in your .wsgi file, point to the location of your app using the sys.path.insert() function. No activation of the virtual environment is needed in the .wsgi file.

Bidarka answered 16/7, 2024 at 21:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.