pythonflaskvirtualenvmod-wsgi

How to get mod_wsgi to pick up my virtualenv


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?


Solution

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