Python Virtual Environments

Python's virtualenv package provides a way to create a local, clean environment well-suited to development, or adding third-party modules as a non-root user.

1. Creating the Virtual Environment

To create a new virtual environment, make sure that the virtualenv module is installed (it is by default on patas), and run the command:

$ python3 -m venv <path_to_virtualenv>

Where <path_to_virtualenv> will create a new directory containing the symlinked python installation.

2. Activating the Virtual Environment

Now that your virtual environment is created, there are two ways to go about activating it:

  1. source <path_to_virtualenv>/bin/activate
    • If you wish to manually change into a virtualenv, you may use the source command above.
    • The source command will run the commands in the "activate" file, and set your default python environment to work within that environment as long as your shell is open.
    • Use this command in any bash script for code you wish to submit for a course that uses a custom virtual environment, to make sure your virtual environment is used.
  2. Edit your ~/.bash_profile
    • If you would like to use a virtualenv as the default python environment when you log in, you may edit your ~/.bash_profile file and add the source command above at the end of the file.
    • This will cause your virtualenv to be activated by default whenever you log in

3. Using the Virtual Environment

You may now proceed to call your scripts using "python <path_to_script>" and rest assured that the version of python being invoked is the one in your virtual environment.

You may also install new modules using the pip command in your virtualenv. For example:

$ pip install nltk

Your virtual environment will contain no additional modules by default, so modules like nltk and tensorflow that may be present in the parent install will not be visible to the virtual interpreter. This is actually a good thing, as it helps you to…

4. Creating a requirements.txt

If you are using third-party modules, and want others to be able to run your code, the standard way of indicating what third party modules are used is in a requirements.txt file. Such a file can be used by pip to install all the necessary requirements in a simple command:

pip -r requirements.txt

To generate such a file, you can use the command:

pip freeze > requirements.txt

The "freeze" command generates a list of all installed modules, at the exact versions that are installed (using '==' to specify the exact version. You may also use '<=' or '>=' to specify earlier or later versions, if you want to allow more flexibility)

Topic revision: r1 - 2018-09-20 - 20:00:18 - rgeorgi
 

This site is powered by the TWiki collaboration platformCopyright & by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback
Privacy Statement Terms & Conditions