Should I activate my Python virtual environment before running my app in upstart?

Question:

I am working through the process of installing and configuring the Superset application. (A Flask app that allows real-time slicing and analysis of business data.)

When it comes to the Python virtual environment, I have read a number of articles and how-to guides and understand the concept of how it allows you to install packages into the virtual environment to keep things neatly contained for my application.

Now that I am preparing this application for (internal) production use, do I need to be activating the virtual environment before launching gunicorn in my upstart script? Or is the virtual environment more just for development and installing/updating packages for my application? (In which case I can just launch gunicorn without the extra step of activating the virtualenv.)

Asked By: AdamsTips

||

Answers:

You should activate a virtualenv on the production server the same way as you do on the development machine. It allows you to run multiple Python applications on the same machine in a controlled environment. No need to worry that an update of packages in one virtualenv will cause an issue in the other one.

If I may suggest something. I really enjoy using virtualenvwrapper to simplify the use of virtualenvs even more. It allows you to define hooks, e.g.: preactivate, postactivate, predeactivate and postdeactivate using the scripts in $VIRTUAL_ENV/bin/. It’s a good place for setting up environmental variables that your Python application can utilize.

And a good and simple tool for process control is supervisord.

Answered By: Mateusz Kleinert