1

I've been trying to get this to work, and have searched everywhere and read page up and page down, but doesn't seem to find an answer.

I have Apache with mod_wsgi and a simple test Flask application.

I got it working using this: https://www.jakowicz.com/flask-apache-wsgi/

Then I somehow found that Apache mod_wsgi used the system python and I want to use venv (https://docs.python.org/3/library/venv.html).

My application is in a directory with the normal directory structure of the venv, but how do I get my application to use that?

I found this: http://blog.dscpl.com.au/2014/09/using-python-virtual-environments-with.html

But if I put in python-home my application fails.

A couple of questions: How do I find the Python version that my app is using? How do I find my mod_wsgi version? How do I get my app to use my venv?

I'm new to Python and WSGI, I have mostly worked with PHP.

Hope someone can explain to me what to do...

-- Ronni

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
Ronni
  • 25
  • 1
  • 3
  • Current Python version is attained by `which python`. As for mod_wsgi version, I'm insure. Lastly, information on virtual environments is here: https://virtualenv.pypa.io/en/stable/ . You can use pip to install virtualenv: `pip install virtualenv`. When installed, you can simply create a virtual environment with the following command: `virtualenv venv` will create a virtual environment folder named 'venv'. When the venv is created you need to activate it: `. /venv/bin/activate`. Now, if you enter `which python` you'll notice that it's a local copy! – amitchone May 24 '18 at 18:53
  • Hi Adam, I've already installed and configured Python venv and have a virtuel environment, but I don't know how to use it with Apache and mod_wsgi. Also it's how I see the Python version that Apache, mod_wsgi uses, how do I execute a command to show the Python version on a webpage that Apache, mod_wsgi and Flask creates; the venv I know is 3.5 – Ronni May 25 '18 at 07:26
  • I've posted a more detailed answer – amitchone May 25 '18 at 10:15

3 Answers3

1

How do I find the Python version that my app is using?

See the documentation:

it provides you a test you can use.

How do I find my mod_wsgi version?

Use the mod_wsgi.version key from the WSGI environ dictionary. See reference to this in:

You can also use:

import mod_wsgi
print(mod_wsgi.version)

How do I get my app to use my venv?

Documentation on using virtual environments can be found at:

As it states, if your mod_wsgi uses system Python and you want to use a different Python installation or version, you cannot force it to use that other installation or version. The mod_wsgi binary must be compiled against the specific Python version you want to use.

Because system mod_wsgi packages are usually ancient and not for the version of Python you want to use, you should uninstall the system mod_wsgi package and install mod_wsgi from source code against the Python version you want to use. For that the easiest install method is using pip install mod_wsgi. See:

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • That seemed to be a lot of work just to use the newest version. I found that proxying the request through Apache to the Flask application was a bit easier; mostly because it just worked the first time. Are there any advantages/disadvantages doing it one way or the other? – Ronni Jun 06 '18 at 14:54
  • You could also just ``pip install mod_wsgi`` then run ``mod_wsgi-express start-server`` with your Flask app and proxy it to it. So don't run it in your main Apache. The Flask builtin development server should not be used for production sites. – Graham Dumpleton Jun 06 '18 at 23:20
0

Continuing the discussion in the comment section of your question:

Assume the following directory with a virtual environment created in the folder /venv:

- main.py
- /static
  - /js
  - /html
  - /css
- /venv
  - /bin
    - activate

To activate the virtual environment, thus using the local copy of Python (as opposed to your global copy), the following command must be used:

. /venv/bin/activate

the . essentially tells the terminal window that the activate file, located at /venv/bin (assuming we're in the top level of the above directory), must be executed. You'll know that the command is successful when you see the string (venv) at the start of a new line in your terminal window.

Now, the which command will confirm that you're now using the local copy of Python:

which python

Now your virtual environment is activated, you can use pip to install any module you wish locally to this virtual environment. You can specify to install a certain version of a module if you wish, or just grab the latest. The version of Flask or Apache that you install depends on what you specify when installing.

Lastly, the command python --version will tell you the version of this copy of Python 2. python3 --version will do the same for Python 3. Whenever you execute a script using this local copy of Python, it will be using this Python version.

To get Python version from within a script:

from sys import version_info   
print version_info

Output (will depend on your version, but will look something like this):

sys.version_info(major=2, minor=7, micro=13, releaselevel='final', serial=0)
amitchone
  • 1,630
  • 3
  • 21
  • 45
-1

The best and most elegant way, I find, is simply specifying the python interpreter that lies within the environment itself. Just start your wsgi file with:

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

Et voilà.

jytou
  • 510
  • 4
  • 7
  • 1
    I noticed this answer from you in at least 3 different questions - sadly it doesn't work for me and after some research its not recommended to rely on this too ... –  Mar 31 '22 at 22:42
  • @ningelsohn well it did (and still does) work for me. I suppose every circumstance has a different solution. – jytou Apr 02 '22 at 00:12