Skip to main content

Command Palette

Search for a command to run...

Virtual Environments

A virtual environment is a tool used to isolate specific Python environments on a single machine, allowing you to work on multiple projects.

Updated
•2 min read•View as Markdown
Virtual Environments
S
I build tools that make developers faster and help startups grow. I specialize in building agentic workflows, oss infrastructure, high-performance web systems, and creating technical content. I've spent years building products used by thousands, from developer-facing infrastructure at Keploy and Wizdom to building the user base for early-stage startups. My work sits at the intersection of agentic AI, open-source ecosystems, and growth engineering.

What is a Virtual Environment?

A virtual environment is a tool used to isolate specific Python environments on a single machine, allowing you to work on multiple projects with different dependencies and packages without conflicts. This can be especially useful when working on projects that have conflicting package versions or packages that are not compatible with each other.

How to create it?

To create a virtual environment in Python, you can use the venv module that comes with Python. Here's an example of how to create a virtual environment and activate it:

# Create a virtual environment
python -m venv myenv

# Activate the virtual environment (Linux/macOS)
source myenv/bin/activate

# Activate the virtual environment (Windows)
myenv\Scripts\activate.bat

Once the virtual environment is activated, any packages that you install using pip will be installed in the virtual environment, rather than in the global Python environment. This allows you to have a separate set of packages for each project, without affecting the packages installed in the global environment.

How to deactivate it?

To deactivate the virtual environment, you can use the deactivate command:

# Deactivate the virtual environment
deactivate

It's just that simple!!

The "requirements.txt" file

In addition to creating and activating a virtual environment, it can be useful to create a requirements.txt file that lists the packages and their versions that your project depends on. This file can be used to easily install all the required packages in a new environment.

To create a requirements.txt file, you can use the pip freeze command, which outputs a list of installed packages and their versions. For example:

# Output the list of installed packages and their versions to a file
pip freeze > requirements.txt

To install the packages listed in the requirements.txt file, you can use the pip install command with the -r flag:

# Install the packages listed in the requirements.txt file
pip install -r requirements.txt

Using a virtual environment and a requirements.txt file can help you manage the dependencies for your Python projects and ensure that your projects are portable and can be easily set up on a new machine.

Conclusion

Thanks for reading this blog!! I hope you have learnt something new today and I wish you an amazing day ahead ❤

A

This is a good article, but you need to mention that you don't always want to do a freeze on the requirements. Sometimes it is better to just you, yourself specify the version of the libraries you are going to use. I personally don't like using it, but that is more of a developer choice. Knowing the version of your libraries makes you know your project better.

3
S

Thanks for saying that, I really appreciate your valuable suggestion!!

2

Python : Advanced

Part 16 of 19

In this series, I will be discussing about some complicated/under-used features of Python, important libraries such as pandas; and also OOPs, Data Analytics and AI-ML.

Up next

Sets in Python

Sets are unordered collection of data items, which are also present in Python. We will learn about them today.