Site icon TechVidvan

Python Virtual Environment

python virtual environments and packages

Packages and modules not found in the standard library are often used in Python applications. Applications will occasionally require a specific version of a library because they may have used an outdated version of the interface when developing them or because they need to solve a particular issue.

This implies that it might not be possible for a single Python installation to satisfy all of an application’s requirements. When two applications have conflicting requirements, such as when program A requires version 1.0 of a module but application B requires version 2.0, neither version can be installed without the other application being unable to function.

Making a virtual environment—a self-contained directory tree including a Python installation for a certain version of Python and several additional packages—is the answer to this issue.

Then, various apps can utilize various virtual environments. Application A can have its virtual environment with version 1.0 installed while Application B has a different virtual environment with version 2.0 installed to resolve the preceding example of conflicting requirements. Application A’s environment won’t be impacted if Application B needs a library to be upgraded to version 3.0. Let us learn more about Python virtual environment.

What is Virtualenv?

A virtual environment is a Python utility for dependency management and project isolation. Instead of being deployed worldwide, they enable the installation of Python site-packages (third-party libraries) locally in an isolated directory for a specific project (i.e. as part of a system-wide Python).

All of that is fantastic, but what exactly is a virtual environment? A directory with three essential components is all that a virtual environment is:

Why Use Virtual Environment?

Virtual environments make it simple to resolve a wide range of potential problems. In particular, they help you with

How Does a Python Virtual Environment Work?

Venv is the name of the module that is used to build and control virtual environments. Normally, venv will install the most recent version of Python that is accessible to you. If your computer has numerous versions of Python installed, you can choose one by typing python3 or your preferred version

The venv module of Python is used in the steps of this tutorial to build virtual environments. Since Python 3.5, using this standard library module to build virtual environments is the accepted best practice.

Venv is a great option for simple usage because it is pre-packaged with your Python installation. With that in mind, you’re prepared to start this tutorial’s initial virtual environment creation.

Creating Virtual Environment in Python

It’s best to first construct a virtual environment whenever you’re working on a Python project that utilizes external dependencies that you’re installing with pip:

PS> python -m venv venv

You may need to specify the complete path to your Python executable if you’re using Python on Windows and haven’t set the PATH and PATHEXT variables:

PS> C:\Users\TechVidvan\AppData\Local\Programs\Python\Python310\python -m venv venv

The system path displayed above presupposes that you used the Python downloads page’s Windows installer to set up Python 3.10 on your computer. Your system’s Python executable may be located in a different location. The where.exe python command can be used to locate the path while working with PowerShell.

Activating Virtual Environment in Python

Your project now has a unique virtual setting. Typically, you’ll activate the environment by running a script that is included with the installation before you begin using it:

PS> venv\Scripts\activate
(venv) PS>

Make sure you are in the folder containing the virtual environment you have established before executing this command.

When the name of your virtual environment—in this case, (venv)—is visible in your command prompt, your virtual environment is operational. You’re prepared to install your external packages at this point.

Installing Virtual Environment in Python

Your virtual environment is now ready for use, and you can add any external dependencies your project requires:

(venv) PS> python -m pip install <package-name>

The default command to use when installing external Python packages through pip is this one. Because the virtual environment was first established and turned on, pip will set up the packages in a separate location.

The installation of your packages into your virtual environment is now possible. You started by making a Python virtual environment called venv and activating it in the current shell session to get to this stage.

Every Python package you install will end up in this contained environment rather than your global Python site packages as long as you don’t close your terminal. As a result, you may now work on your Python project without being concerned about any dependency issues.

Deactivating Python Virtual Environment

You can deactivate this virtual environment when you’re done with it:

(venv) PS> deactivate
PS>

The deactivate command causes your command prompt to reset to its default state. This modification indicates that you have left your virtual world. You will interact with your locally configured Python environment if you use Python or pip right now.

You must once again run the virtual environment’s activate script if you want to enter a previous virtual environment that you’ve built.

You’ve now studied the fundamentals of using Python virtual environments. If that’s all you require, good luck with your future creations!

You should continue reading if you want to learn more about what happened, What a Python virtual environment is, why so many tutorials demand that you create one, and why this is necessary. You’re going deep soon!

Managing Environment in Python

Requirements Files:

The simplest way to enable people to reproduce our work is to put a requirements file in the root directory of our project (top directory). Pip freeze, which displays a list of installed third-party packages along with their version numbers, will be used to accomplish this.

(venv) % pip freeze
numpy==1.15.3 

Likewise, create a file we’ll name requirements.txt with the output in it.

(venv) % pip freeze > requirements.txt

When we update or install a new package, we may use the same command to update our needs file.

Anyone we share our project with can now use our requirements.txt file to duplicate our environment and run our project on their PC.

Conclusion

You developed a solid grasp of python virtual environment throughout the lesson, including what they are, why you need them, how they work internally, and how to manage them on your system.

Exit mobile version