A) What is virtual environment
A virtual environment is an important tool that separates the dependencies with the creation of a separate and isolated environment for different projects. A Virtual environment is very important whenever someone wants to implement a particular project because it will make dependencies remain separated and may not interfere with other projects and avoid issue creation or crashes. It is also easy for anyone to create virtual environment and delete without hampering dependencies of another project. If someone installs all dependencies in base environment, then base environment cannot be used for different projects because their package version requirement will be different and may create issues. It is advisable for everyone to use a different virtual environment whenever they want to implement a new project.
A Python virtual environment is essentially a self-contained ecosystem that comprises two key elements:
· The Python Interpreter: Virtual environments in Python provide isolated environments with their own Python interpreter. These environments are separate from the system-wide Python installation. This isolation guarantees that any modifications made within a virtual environment won’t affect the global Python setup.
· Library Folder: Virtual environments have separate directories for isolated third-party libraries, ensuring no interference with other environments or the global Python setup, allowing for different library sets and versions.
B) Installation of virtual environment:
Installation of virtual environment in python environment have different ways and with different steps as follows:
1. Creating a virtual environment using Anaconda:
Virtual environment with the help of anaconda is straightforward process. It provides a command-line tool called conda that makes it easy to manage virtual environments. We can create virtual environment in anaconda with following steps:
· Open Anaconda Prompt: If you are using Windows, then open the “Anaconda Prompt” from the Anaconda Navigator or from the Start menu.
On macOS or Linux, open a terminal.
· Create a New Virtual Environment: Use the conda create command to create a new virtual environment. Replace myenv1 with the name you want to give to your virtual environment and specify the Python version you want to use, if desired.
For example, to create a virtual environment named “myenv1” with Python 3.7, then run the command
conda create –name myenv1 python==3.7 anaconda
Or
Conda create -n myenv1 python==3.7 anaconda
· Activate the Virtual Environment: After creating the virtual environment, you need to activate to start using it with the help of conda activate command.
conda activate myenv1
· Deactivate the Virtual Environment: Once work with the virtual environment is over then it is good practice to deactivate it. You can deactivate it using the conda deactivate command.
conda deactivate
2. Creating a virtual environment using the venv package:
The “venv“ is included with Python 3 and provides a simple way to create and manage virtual environments. Follow the steps below to create a virtual environment.
· Open a Terminal/Command Prompt:
Open your terminal or command prompt from the start menu. First make sure that you have Python 3 installed by running the following command.
python –version
· Navigate to Your Project Directory:
It’s a good practice to create virtual environments within specific project directories. Navigate to the directory where you want to create your virtual environment using the cd command.
· Create the Virtual Environment:
Now run the following command to create myenv1 virtual environment. You can replace myenv1 with the name of virtual environment you would like to give.
python -m venv myenv1
This will create a new directory called “myenv1” (or whatever name you specified) that contains the virtual environment.
· Activate the Virtual Environment:
There are different ways to activate virtual environments on different operating systems. On Windows system, you can activate the virtual environment with this command:
myenv1Scriptsactivate
On macOS and Linux systems, use following command:
source myenv1/bin/activate
After activation, your terminal prompt will indicate that you are now inside the virtual environment.
· Deactivate the Virtual Environment:
When you’re done working in the virtual environment, you can deactivate it using the following command:
deactivate
C) Benefits of Virtual Environment:
· Isolation and Independence: Python virtual environments allow you to create isolated spaces for your projects. Each environment acts as a self-contained universe with its own Python interpreter and libraries. This isolation ensures that any changes or updates made within one environment won’t affect others or your system-wide Python installation. This separation is crucial to prevent conflicts between different project dependencies.
· Multiple Python Versions: One of the primary benefits of virtual environments is the ability to work with multiple Python versions simultaneously. You can create separate environments for projects that require different Python versions, whether it’s Python 2.x or Python 3.x. This flexibility is essential when maintaining legacy codebases or transitioning to newer Python releases.
· Customized Dependency Management: With virtual environments, you have granular control over your project’s dependencies. You can install and manage libraries specific to each project without worrying about interfering with other projects. This customization allows you to specify precise versions of libraries and avoid version conflicts, ensuring that your project remains stable and reproducible.
· Clean and Consistent Development Workflow: Virtual environments promote a clean and consistent development workflow. Developers can easily share project requirements by providing a requirements.txt file that lists all dependencies. This file can be used to recreate the exact environment on another machine, ensuring that everyone working on the project is using the same set of dependencies.
· Sandboxing and Security: Isolated environments also enhance security. They prevent unintended changes to system-wide Python packages, reducing the risk of breaking critical system tools or applications. Sandboxing provides an added layer of protection when experimenting with new libraries or tools.
· Resource Management: Virtual environments can be configured with specific resource constraints, such as CPU and memory limits, to optimize performance for your projects. This level of fine-tuning ensures that your applications run efficiently and don’t hog system resources.
D) Important operations in virtual environment
1. Creation of new virtual environment:
Creation of new virtual environment in python follows various steps which are listed and discussed in above section. After creation of virtual environment only we can perform different operations on it.
2. Installation of package into virtual environment:
Virtual environment is mainly created to install dependencies and keep it isolated from different project requirements. If you want to install any package in the virtual environment, then use pip install command. For example, if you want to install NumPy package the install as follows:
pip install numpy
If you want specific version of NumPy package, then specify the version of it as follows:
pip install numpy==1.25.2
The same command can be used in venv as well.
3. Uninstalling any package from virtual environment:
If at specific point, we don’t want package then we can uninstall that package with pip uninstall command.
For example, we want to uninstall NumPy package then use following command.
pip uninstall numpy
The same command can be used in venv as well.
4. Saving list of installed packages in virtual environment:
If we want to save the list of packages installed in the virtual environment as text file on computer, then using pip freeze command it is possible to do so. While doing this file name must be specified.
For example, if you want to save list of packages installed in the virtual environment in the text file named requirement.txt then use following command.
pip freeze >> requirements.txt
Or
Pip list>>requirements.txt
The requirements.txt file will be saved in the current folder.
The same command can be used in venv as well.
5. Display list of packages installed in the virtual environment:
To display which packages are installed in the virtual environment with what version number can be checked in anaconda with the help of pip list or pip freeze command.
The same command can be used in venv as well.
6. Display list of virtual environments available:
As we can create any number of virtual environments in the system, and we want to check list of available virtual environments then use following commands:
In anaconda environment the command is
conda env list
Or
conda info — envs
In venv environment the command is
lsvenv
7. Deletion of virtual environment:
If we want to delete any virtual environment, then use the following command specifying the name of virtual environment in anaconda.
conda remove –n myenv1 –all
There is a different command to remove virtual environment in venv package. First deactivate the virtual environment and then use following command to remove virtual environment named myenv1 as
rm -rf myenv1
rm command is not working in windows then use del command to remove it.
E) Conclusion:
In summary, Python virtual environment is a powerful and a crucial tool for developers and programmers offer a versatile way for managing project dependencies, ensuring compatibility across different Python versions, and maintaining a clean and isolated development environment. They are a best practice for Python developers, enhancing project organization, reproducibility, and overall development efficiency while minimizing conflicts and security risks.