Creating a Virtual Environment in Python, for Python.
Developing Python programs can be tricky, and managing packages and versions adds extra complexity. Using a virtual environment isolates your project’s dependencies, keeping everything organized and stable. Today, I’m revisiting how to use Python virtual environments for development.
Developers use virtual environments for most things that they would sit down to develop or create. The intuition for this is that it isolates the development environment, allowing more control as to what is installed or used within it.
Think of your own device as being the global environment from which you can develop, but it will always have regular updates pushed to it. These regular updates could interfere with or disrupt your current code as they could change syntax and settings, and would become quite tedious to always manage.
Now, having a venv enables the developer to install what they need and keep it at those versions unless they force an update.
Recently, I was introduced to using Docker to create a Linux-based development environment on my laptop while working on this blog.
While I understand Docker at a high level, it has much more depth compared to a simple Python venv. Because of that, I decided to revisit virtual environments to better understand how and why they are used.
Creating a Python Virtual Environment
Modern versions of Python (3.3 and above) include the venv module, so no need to install the package. So you can skip this step:
1
pip install virtualenv
Now, create a directory for your project (you can name it anything you like). Then open your CLI, navigate into the project folder from inside the terminal, and run the following code:
1
2
3
4
cd project_folder
# python -m venv (application) (folder name for venv)
# example:
python -m venv venv
This command now creates all the requirements needed to launch a virtual environment by giving your project its own Python interpreter placed inside venv. All we now need to do is activate it, which I will explain in the following steps.
Before we get into that, I would suggest taking a minute to browse the folder and its contents, as you’ll begin to get an idea of what sort of files are inside.
Activating the Virtual Environment
For the most part, that is how you would install virtual environments for Python projects, and you would do that for each project you have to ensure they all have separate dependencies or packages.
1
2
3
4
5
6
7
8
# macOS / Linux:
source venv/bin/activate
# Windows (PowerShell):
venv\Scripts\activate
# Windows (Git Bash):
source venv/Scripts/activate
This is just using one of the files located within the initialised venv folder that contains the correct script needed to open the isolated virtual environment and start developing from.
Once activated, your terminal prompt should change to show the virtual environment name or (venv), for example:
1
(venv) user@machine:~/project_folder$
This is to tell the user that they are now in virtual mode and any Python or pip commands will only apply to that environment.
On my first attempt at doing this, I found it a little difficult as I wasn’t sure whether I should do it using the main terminal or using the one in VS Code.
After reading into it a bit, I found it easier to use the terminal from within VS Code to perform all the actions, as it keeps everything neat and organised.
You can open it by clicking the terminal icon in the bottom-left corner of VS Code:
From here on out, we can activate the virtual environment whenever we need and run all the commands as usual. These will only perform or commit their actions inside the virtual environment and not the global environment.
To save yourself the trouble of having to do all of this, you can use an alternate method.
This is installing and using an IDE like PyCharm, which manages virtual environments automatically for each project, but I recommend always downloading software from legitimate sources.
Using the Virtual Environment
Once we have our virtual environment active, we can install, remove, update, and work with any Python packages.
All the work we do would be from within the virtual environment and wouldn’t really interrupt the main device, and its Python packages.
1
(venv) user@machine:~/project_folder$: pip install package_name
The package will be installed only inside the virtual environment, not globally on your system.
To check which packages are outdated, you can run:
1
pip list --outdated
You can then update a specific package if needed:
1
pip install --upgrade package_name
Updating packages selectively is generally considered safer than upgrading everything at once, especially for existing projects.
Managing Dependencies with requirements.txt
A requirements.txt file can be viewed as a repository with all the dependencies or packages and their versions of a Python project.
Think of an instance where you may have the virtual environment deleted or even create a new virtual environment, and you need to install hundreds of packages and their specific versions on the virtual environment.
While you can install them all singularly using a repetitive method, you should extract all the information to a file and use that file as an installation requirements file.
When you instruct pip to install the requirements, it will automatically install them all for you into your virtual environment.
Important: Make sure your virtual environment is activated before running the following command.
1
pip freeze > requirements.txt
This command captures a snapshot of all installed packages and their versions and saves them to a file which you can then use to install onto your virtual environment.
If you’d ever like to manually create a list of requirements or insert a specific package into your requirements file, this is the format you would follow:
1
2
# Package name == version number:
asgiref==3.11.0
When you want the compiled list of packages to be installed in your venv you would place the file inside the project directory and navigate the terminal to the same location and run the following code:
1
pip install -r requirements.txt
You can also upgrade packages listed in the file if required:
1
pip install --upgrade -r requirements.txt
Comparing Environments
If, for whatever reason, you would like to compare two different lists of installed packages to check if they are only being installed on your virtual environment or between two virtual environments, you can perform the following code on both terminals:
1
pip list
Closing the Virtual Environment
When you are finished working, you can deactivate the virtual environment by running:
1
deactivate
This returns your terminal to the global Python environment.
End of Topic
This was good to gain a deeper insight into it again. There isn’t a whole bunch to know from using this platform. The key takeaway here really is that we should always develop in isolated environments for any programming language. This helps keep the project secure and everything organised.
It also helps developers know what packages and versions they’re using, from experience and time spent on coding, they’ll begin to also know the differences between packages and what they need and don’t need.

