19

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?

3 Answers 3

12

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")
0
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

20
  • 1
    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. Mar 9, 2017 at 0:18
  • 1
    You need to install the 'dev' package for Apache. See system requirements in pypi.python.org/pypi/mod_wsgi Mar 9, 2017 at 0:38
  • ok as per the doc, installed apache2-dev and checked it was working using mod_wsgi-express start-server with my python 3 venv- Mar 9, 2017 at 1:44
  • 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. Mar 9, 2017 at 1:56
  • 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? Mar 9, 2017 at 2:13
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.