Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 799
Timothy Lombard Avatar asked Mar 08 '17 02:03

Timothy Lombard


People also ask

How do I find my virtualenv?

how can I find the path of virtualenv python ,built with this tutorial? Activate the created env first: workon cv . Then issue which python , this will show you the path to the executable. Also, when an env is activated, issuing echo $VIRTUAL_ENV will show you the path to the directory containing env files.

How do I activate virtualenv activation?

To activate virtualenv on Windows, first, install the pip. For this purpose, you can download and execute the latest Python installer. Next, install and create virtualenv on Windows using the pip package manager. Then, activate it using the “venvironment\Scripts\activate” command.

How do I know if virtualenv is activated?

Note: Before installing a package, look for the name of your virtual environment within parentheses just before your command prompt. In the example above, the name of the environment is venv . If the name shows up, then you know that your virtual environment is active, and you can install your external dependencies.


2 Answers

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.

like image 155
jytou Avatar answered Oct 10 '22 02:10

jytou


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")
like image 24
Aaron de Windt Avatar answered Oct 10 '22 02:10

Aaron de Windt