Virtual Environment in Python. Topic – 29

Virtual Environment in python

A Virtual Environment is a tool that helps isolate multiple Python environments on a single machine. It is used to work on multiple projects with different dependencies and packages without conflicts. This is particularly useful when working on projects that have conflicting package versions or incompatible packages.

Using venv module, we can create a virtual environment. Let’s see the command:

# Virtual environment banaye
python -m venv myenv
# Virtual environment activate karein (Linux/macOS)
source myenv/bin/activate
# Virtual environment activate karein (Windows)

Deactivate Virtual Environment:

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

To deactivate the Virtual Environment, you can use the deactivate command:

# Deactivate the Virtual Environment

“requirements.txt” file:

In addition to creating and activating a Virtual Environment, it can be useful to create a requirements.txt file listing 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 easily set up on a new machine.

If you don’t know the “Finally Keywords In Python” just click on Read More.

I hope you understand “Virtual Environment in Python” Need guidance or have questions? Drop a comment below. Share your email for a personalized touch—I’ll send exclusive resources and answer queries. Let’s code together, creating efficient code and memorable learning moments! 🚀

Finite number of  lines code and infinite possibilities

Programmer, Arpan Bera

Leave a Reply

Your email address will not be published. Required fields are marked *

Arpan Bera

Coding is like Love – everyone can start, but only a few finish without errors

Keep Updated to our News and Blog