# Creating a Python Virtual Environment

**URL:** <https://ask.cyberinfrastructure.org/t/creating-a-python-virtual-environment/2063>\
**Category:** Discussion Zone\
**Tags:** python\
**Created:** [September 27, 2021, 4:34pm UTC](https://ask.cyberinfrastructure.org/t/creating-a-python-virtual-environment/2063 "2021-09-27T16:34:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Nadeau](https://ask.cyberinfrastructure.org/user_avatar/ask.cyberinfrastructure.org/christopher_nadeau/32/618_2.png) [@Christopher\_Nadeau](https://ask.cyberinfrastructure.org/u/Christopher_Nadeau)\
**Post date:** [September 27, 2021, 4:34pm UTC](https://ask.cyberinfrastructure.org/t/creating-a-python-virtual-environment/2063/1 "2021-09-27T16:34:58Z")

</div>

# What is a Virtual Environment?

A virtual environment is a way to isolate a specific working environment from the rest of your system. It is highly suggested to use a Python virtual environment when working on computing projects, as it allows the user to tightly control the packages available to the project, and will allow multiple projects that may have conflicting software requirements to be worked on from the same machine.

Virtual environments are also fairly easy to use and create, especially when working with Python 3, where the `venv` package is now included in all standard Python 3.x installations.

This tutorial will cover the creation of a virtual environment on UNIX-based systems, in particular Linux derivatives.

# Step 1: Getting Python

It is highly suggested to use an alternate version of Python from the system default to base your virtual environment off of, as this version can be easily overwritten. To do this, it depends on your use case.

## On a cluster

The simplest way of getting an alternative version of Python is to simply find an acceptable version of Python supplied via the ` module` command. To see the list of modules available on the cluster you are working with, use the command

```auto
module avail

```

Then, load the desired version of Python with

```auto
module load Python/version

```

where `Python/version` would be replaced by the specific module you were attempting to load.

A note with this method: If using `sbatch` to submit jobs, you will need to load the Python version before activating your virtual environment. This is accomplished by repeating the above process of loading a module after specifying the SLURM options at the head of your `sbatch` script.

## On a local machine:

When the machine you are working on is under your control, the easiest way is to simply install another version of Python alongside the system version of Python, and specify to use this version when creating the virtual environment.

To do this, first create the location of your alternative Python install. For this tutorial, we will create it as a hidden subdirectory of the default `$HOME` directory.

```auto
mkdir $HOME/.localpython

```

Next, use `cd` to enter this directory:

```auto
cd ~/.localpython

```

and then use `wget` to download the tarball of your desired Python version:

```auto
wget https://www.python.org/ftp/python/3.x/Python-3.x.tar.xz

```

where `3.x` would be replaced by the desired version of Python.

Next, extract and install to the created local directory:

```auto
tar -xvf Python-3.x.tar.xz
cd Python-3.x
./configure --prefix=$HOME/.localpython
make && make altinstall

```

## Step 2: Creating the virtual environment

If you do not already have a folder where your project is/will be stored, create one as a subdirectory of your HOME directory, or in another convenient place

```auto
mkdir ~/project-folder

```

Now, create the virtual environment:

### Cluster:

`python3 -m venv path/to/project/folder`

### Local machine:

```auto
$HOME/.localpython/bin/python3 -m venv path/to/project/folder

```

The virtual environment may then be activated by running

```auto
source path/to/project/folder/bin/activate

```

You will know if the virtual environment successfully launched if your terminal now looks like this:

```auto
(project-folder) [user@hostname ~]$

```

---

<div class="post-metadata">

**Author:** ![chris\_blanton1](https://ask.cyberinfrastructure.org/user_avatar/ask.cyberinfrastructure.org/chris_blanton1/32/962_2.png) [@chris\_blanton1](https://ask.cyberinfrastructure.org/u/chris_blanton1)\
**Post date:** [May 8, 2023, 3:37pm UTC](https://ask.cyberinfrastructure.org/t/creating-a-python-virtual-environment/2063/2 "2023-05-08T15:37:22Z")

</div>

As an update to the above method of creating a virtual environment, it should be mentioned that Anaconda’s conda environments are a method to create virtual environments for Python and other software (including R).

In addition, using `poetry` can be very useful for project-based environments. See [Poetry - Python dependency management and packaging made easy](https://python-poetry.org/) for full instructions on how to use Poetry.

---

<div class="post-metadata">

**Author:** ![jsimms](https://ask.cyberinfrastructure.org/user_avatar/ask.cyberinfrastructure.org/jsimms/32/933_2.png) [@jsimms](https://ask.cyberinfrastructure.org/u/jsimms)\
**Post date:** [May 11, 2023, 2:01pm UTC](https://ask.cyberinfrastructure.org/t/creating-a-python-virtual-environment/2063/3 "2023-05-11T14:01:31Z")

</div>

I agree that conda would seem to present a more straightforward option in most cases. We use it and it seems to work well.
