Activation (activate_this.py) of python venv wont work or throw error

1.7k views Asked by At

Soo, I created a small Flask Website which I wanna deploy on my Debian 10 server with Apache 2.4

At first I followed a tutorial and created the apache2 conf file and the .wsgi file. Since that didn't work well, I got the error "ModuleNotFoundError: No module named 'flask'" so I set up an virtual enviroment with

python3 -m venv venv

and changed my .wsgi to

#!/usr/bin/python3
import sys
import logging

activate_this = '/home/bot/Club-Manager/Webinterface/venv/bin/activate_this.py'

with open(activate_this) as file_:
        exec(file_.read(), dict(__file__=activate_this))

logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/home/bot/Club-Manager/Webinterface/")

import app as application

Now I get

Failed to exec Python script file '/var/www/html/Webinterface/Webinterface.wsgi'
Exception occurred processing WSGI script '/var/www/html/Webinterface/Webinterface.wsgi'., file "/var/www/html/Webinterface/Webinterface.wsgi", line 8, in <module>, 
exec(file_.read(), dict(__file__=activate_this))
File "<string>", line 28, in <module>
AttributeError: 'str' object has no attribute 'decode'

At first the "activate_this.py" didn't exist for me, some research told me to just copy it from the lib/virtualenv folder which I tried (The one in the venv and the one in ~/.local/). After some more research I found out that you should create the venv with the same python version you're using to run it (Duh), I check that and created the venv with python3 instead of just python because python3 and python2 are installed (even if "python" gets me to python 3.7, but /usr/bin/python is python 2). I also tried the "activator.py" file from the venv/lib/virtualenv but this also didn't work (well it did but threw the also the flask not found error)

I checked and reinstalled the dependencies in the venv, I checked if they are install for python3 and they are.

So now I'm pretty lost, I guess the problem is the "activate_this.py" file but I dont know how I get a functional one or what else could lead this to break. The "Apache2 Part" to forward my request from the domain to the python script seems to work (well I guess because why else could I see the errors) so I didn't append the apache conf, but if needed I can post it ofc. I also checked the permissions for the folders, /var/www/html/Webinterface is a symlink to /home/bot/Club-Manager/Webinterface and all of them got bot:www-data which is the user and group apache2 should use

Thanks for any tips in advance

1

There are 1 answers

0
jytou On

I know this is a year old, but in case anyone has the same problem...

the easiest/fastest/cleanest way I've found is to simply reference the python interpreter from within the venv from your wsgi file, like this:

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

From there, your wsgi app will be using the corresponding python, no need for activating, searching for packages, etc.