Setting Up A Virtual Environment In Python.

Today, we explore an interesting and highly practical topic, so do well to turn on your PCs or Macs, put on your coding glasses and keep a nice hot cup of coffee by your side. If you've got that all covered, then let's get into the fun stuff. Here we go.

NOTE*: For this tutorial, I'll be typing all commands on my Powershell terminal on the windows 11 Operating System. So, if you're using a Linux terminal (bash or zsh) or a mac terminal, and the commands I use do not work for you, then please feel free to do your research and make it work. With that said, I'll still do my best to ensure that I provide the appropriate commands for the above-listed platforms.*

Steps to set up a virtual environment

Step 1: Create a project directory and cd into it

You can name this directory anything you like, but I'm going to name my directory sample.

For Linux, PowerShell and mac users, type the following command:

mkdir sample
cd sample

For windows command prompt and PowerShell users also, type the following command:

md sample
cd sample

press ENTER each time you time one of the commands above. With that done, move on to the next step:

Step 2: Install virtualenv tool if you don't have it installed already (OPTIONAL)

If you don't yet have the virtualenv tool used to create virtual environments installed on your host python, then type the following command into any terminal you're using:

pip install virtualenv

and then press ENTER. Let's move on:

Step 3: Create a virtual environment in the directory you just created

In sample (or whatever you named your directory), it's time to create the actual virtual environment. Now, the command to do just that is shown below:

python -m venv <virtual env name>

I want to call my virtual environment sample_env. So, with that in mind, I'd type the following into my terminal (Powershell):

python -m venv sample_env

If you're following along on a Linux or Mac, then just change python to python3 and run the command (as is appropriate)

and then press ENTER and type ls, you should see a directory named sample_env in your current working directory. This specific directory contains the files and scripts necessary to create and activate any virtual environments we create. Now, it's time for the next step.

Step 4: Activate your virtual environment

Now that we've created the virtual environment, let's proceed to activate it. To do that, you'll need to type any one of the following commands depending on your operating system or the terminal you use:

Mac:

source <virtual env name>/bin/activate

Windows (Command Prompt):

<virtual env name>\Scripts\activate.bat

Windows (Powershell):

<virtual env name>\Scripts\Activate.ps1

and then press ENTER. So in my case, I typed in the following:

sample_env\Scripts\Activate.ps1

NOTE*: If you run into an error with an error message that contains the following line:* cannot be loaded because running scripts is disabled on this system. , then please check out this link for the solution to this problem, solve it and then try again.

Your virtual environment is now running.

And that is how you set up a virtual environment. To leave the virtual environment, you simply need to type the following into the terminal:

deactivate

and that's it. There is one last step though, albeit this is not a compulsory step.

Step 5: Create a requirements file (OPTIONAL)

After installing all the packages you require for your project in your virtual environment it's good practice to create a requirements file containing instructions that instruct a python runtime on how to recreate that same environment automatically. To create the requirements file, simply type the following in the terminal:

pip freeze > <name of requirement file>.txt

and to make python create an exact copy of the virtual environment on your host machine, simply type the following:

pip install -r <name of requirement file>.txt

The benefit of creating a requirement file is that it makes it possible to share your virtual environment with others easily as you don't need to transfer the entire virtual environment, all you have to transfer is a very simple text file that the other person can then use to install all dependencies with a single command as shown above.

Alright, I hope that you now understand how to set up and work with a virtual environment. Thank you very much for reading this and I'll see you again next time but until then:

Keep Learning
Keep Growing