Virtual Environments in 30 Seconds
Tags: tutorial, python, beginners, productivity
Published: 2022-03-17 15:22:15

Virtual Environments are a great way to manage dependencies, and to keep your projects clean and isolated from one another, this posts describes how you can create virtual environments using two of the post popular libraries, along with a quick TLDR (Too long, didn't read) at the top, for people in a hurry.

For the people who came for the 30 seconds, here you go

### TLDR

#### Python3 Virtual Environment
```
python3.9 -m venv <venv_path>
```
`<venv_path>` being the path where you want the venv generated.


#### Python2 Virtual Environment
```
pip install --upgrade virtualenv
virtualenv -p <interpreter_path> <venv_path>
```
You can use `where python<version>` to find the paths of your interpreters, for me
```
 ➜  where python2.7
/usr/bin/python2.7
/bin/python2.7
```
Replace the `<interpreter_path>` with the path to your interpreter, and replace `<venv_path>` with where you want it to be generated.

#### Basic Commands

Activating Venv -
```
source <venv_path>/bin/activate
```

You can use pip install as you would normally -
```
pip install <package_name>
```
And to deactivate -
```
deactivate
```
Typing it into your terminal while you're in a virtual environment will deactivate it.

------

# Virtual Environments in Python

### Why Virtual Environments are useful, and why you should use them

Virtual Environments are preferred by many for a variety of reasons, the largest of them being to avoid conflicts between packages. For example, if I want to install some packages which have the same namespace (or the same import name, to simplify), it's not feasible to uninstall and reinstall one another depending on what application I have to run at the time, the same applies to versioning conflicts, if Application A needs version `0.0.1` of my package, and Application B needs version `0.0.2`, it's simply a bad idea to keep installing and uninstalling. 

This is where virtual environments come into play, they allow you to, as the name suggests, create entirely new, isolated environments for your code to run in, this allows me to install packages required by Application A, and packages required by Application B without there being any conflicts between the two. They're also useful for generating `requirements.txt`s.

They're a good idea to use because they keep your packages separate from one another, and make it easier for others to deploy or run your code.

------

There are multiple ways of creating Virtual Environments (shortened to 'venvs') in Python, there are two major libraries, both with their own benefits. 

### Using the Venv package

`venv` only supports one version, but is a part of Python's Standard Library, meaning that it comes pre-installed and ready to use out of the box.

To create a virtual environment, simply do
```
python3.9 -m venv <path>
```
That's it, you simply need to provide the path to the directory where you want the virtual environment to be created, and all relevant files will be generated.

### Using the Virtualenv package

The next library is `virtualenv`, you need to manually install it using `pip install` but, it supports creating virtual environments with differing versions of python. If you want to run legacy code in a custom environment, or anything of the like, this library is for you.

Begin by installing `virtualenv`
```
pip install --upgrade virtualenv
```
Once the package is installed, you can create a virtual environment for the version of your choosing.
```
virtualenv -p /usr/bin/python3.9 venv
```
Over here, we're invoking the virtualenv command, and providing the `path` (`-p`) argument, the value of this argument is supposed to be the path of the interpreter we want to use.

To find all your installations of python, you can run
```
where python<version>
```
For me, the output was
```
➜ where python3.9
/usr/bin/python3.9
/bin/python3.9
```
Similarly, you can use any of the provided paths as the value of the `-p` flag.

Once you provide the interpreter path, you also need to provide the path to your virtual environment, virtualenv will create a directory at that location populated with the virtual environment's contents.

### Basic Commands to interface with Virtual Environments

Now that you've created your virtual environment, it's time to put it to use. Virtual environment commands are consistent across packages, that means, no matter what package you use to create your venv, the commands to utilize it remain the same.

Opening/Using the virtual environment -
```
source <path_to_venv>/bin/activate
```
This command runs the venv's activation script, which is generated automatically. Once this is run, you'll see a pair of brackets near your terminal with the name of your virtual environment, this signifies that you're now using your venv.

Installing packages -
```
pip install --upgrade <package>
```
Installation remains the same, but now installs to your virtual environment directory rather than your global one.

Exiting the virtual environment -
```
deactivate
```
You just need to type deactivate into your terminal to exit it.

Deleting your virtual environment -
```
rm -r <path_to_venv>
```
There isn't an in-built way to delete virtual environments, a `rm -r` command will work just fine, though.

### How to generate a requirements.txt

You can generate a requirements.txt file by activating your virtual environment, and running
```
pip freeze > requirements.txt
```
This inserts a list of all your packages, and their versions into a file, separated by newlines.
```
aiofiles==0.6.0
aiohttp==3.8.1
aiosignal==1.2.0
aiosqlite==0.17.0
```
For example ^^

----

I hope that was useful, a like and/or follow would be appreciated :)