How to activate a Python virtual environment from a script file

This article describes how to activate a Python virtual environment from a script file.

The method described below assumes that you have already created a Python virtual environment and installed the module (or modules) you want to use. For information about how to do this, please see this article and this article.

Applications that require Python 3 should use the Python Selector. Please contact support if the Python Selector is not available on your server.

Activating a Python virtual environment from a script file

There are numerous modules available to extend Python functionality. To install these modules, you create a virtual environment and use the pip command (or the cPanel Python Selector application).

To actually use these modules in a script or program, you must activate the Python virtual environment that contains them. Depending on your configuration and requirements, you may be able to simply use the virtual environment's bin/activate program for activation, and then run commands from the shell.

In other scenarios, however, you may need to dynamically activate the virtual environment directly from a script or program. One example of such a scenario is a Python CGI script file, which is called directly by the web server. To use a virtual environment's module in such a scenario, use the script to activate the virtual environment directly.

The following sample Python CGI script file demonstrates how to do this. To run this code on your own account, do the following:

  • Replace username with your A2 Hosting account username.
  • Replace application with the name of your Python application.
  • Replace x.y with the Python version of the virtual environment (for example, 2.7 or 3.8).
  • Replace module with the name of a module you have installed in the virtual environment.
  • Replace variable with the name of a variable from the module installed in the virtual environment.

import os
import sys
import pkg_resources

activate_this = str(os.path.dirname(sys.executable)) + '/'
with open(activate_this) as f:
        code = compile(, activate_this, 'exec')
        exec(code, dict(__file__=activate_this))

from module import variable

print ("Content-type:text/html\r\n\r\n")
print ('<html>')
print ('<head>')
print ('<title>Virtualenv test</title>')
print ('</head>')
print ('<body>')
print ('<h3>If you see this, the module import was successful</h3>')
print ('Python version: ' + sys.version)
print ('<br/>')
print ('Python executable: ' + str(sys.executable))
print ('<br/>')
print ('Installed modules: ')
print ([p.project_name for p in pkg_resources.working_set])
print ('<br/>')
print ('</body>')
print ('</html>')

When you run this script from the command line or load it in your web browser, you should receive the following message:

If you see this, the module import was successful

This indicates that the from module import variable statement succeeded, and the virtual environment's variable is now available for you to use in the script. Additionally, the script prints information about the Python environment.

If you do not receive the “successful” message in your browser, try running the script file manually from the command line. For example, if the script file is named script.cgi, type the following command from within the virtual environment:

python ~/public_html/script.cgi

Examine the output and look for any error messages.

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.