Python Virtual Environments, what are they and how to make one?
A virtual environment is an isolated environment which you can use to group the dependencies of a Python project.
Dependencies are pieces of software that need to be installed for a script to run without errors. The script depends on them. The dependencies of a Python file include Python itself and packages, like pandas and matplotlib, that you import and use in the script.
The different projects you make might require different packages or different versions of packages. In that case virtual enviroments can be a solution for you.

By using virtual environments you can:
- keep an overview of what the actual dependencies of a project are which is nice when you want to copy the project
- have different versions of a package on a computer. Code that is written and tested with an older version of a package does not always work with newer versions. So just updating a package to the newest version might cause problems for old projects. By being able to use different versions you can keep your old code working with the older version and make a new project with a newer version at the same time.
How to make a virtual environment for Python?
Note: this method should work for Python versions 3.3 and newer.
When beginning a new project I like to start by making a new directory. I work on Windows so I almost always just use the File Explorer to make a new folder.
For example: C:\Users\BE\Documents\Virtual Environment Demo
Then you need to navigate to this folder in a program in which you can make command line commands like Terminal or Command Prompt.
Here is a Command Prompt (Windows) example of the change directory command to navigate to a directory: cd C:\Users\BE\Documents\Virtual Environment Demo
I actually open the folder in Visual Studio Code and open VSC’s terminal. The project directory I just created and opened is then automatically selected in the terminal.
Then you have to type the command to make a virtual environment which is: py -m venv venv
Other versions that might work for you are: python3 -m venv venv or python -m venv venv
Here is what I believe the command means:
pyis to specify that you want to run Python code-m venvis to specify that you want to run the module venv- The second
venvwill be the name of our virtual enviroment folder
After typing the command you hit enter and wait for the command to finish. Soon after, there should be a folder called venv in your project folder.
How to use a virtual environment for Python?
You can access the virtual environment with another command: venv\Scripts\activate.bat
If you are in the virtual environment you should see (venv) in front of the current directory in the terminal. For example, my terminal window shows: (venv) C:\Users\BE\Documents\Virtual Environment Demo>
When you are in the virtual environment you can install the packages you need for your project. For example, to install the pandas package you run this command: pip install pandas
For more information about packages and how to install them you can read this short article of mine:
To exit the virtual environment just run the command: deactivate.
As an important note, I don’t put my scripts in the venv folder but just in the project folder.
Thank you for reading!
You can get full access to all my posts by joining Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:





