Today here at Arzhost, we discuss how we create new virtualenv python in our windows or Linux. Applications written in Python frequently employ packages and other modules that aren’t included in the standard library. Applications may occasionally need a specific version of the library to complete a job or resolve a defect. How to Create a Virtualenv in Python?
Creating Virtual Environments is Important
Let’s say we are creating two applications that need an older version of the library while other applications in the same Python installation need a newer version. It can occasionally result in a problem.
In other words, not every application’s needs may be met by a single Python installation. Conflicts between the requirements of different applications may arise. For instance, if application A needs version 1.0 and application B needs version 2.0, a conflict will develop and one application won’t be able to execute.
Python offers to create a new virtualenv python in order to satisfy this conflicting requirement. Version 1.0 is the environment for Application A. Application B’s environment is version 2.0, so if we upgrade Application B to Version 3.0, Application A’s environment won’t be impacte.
Construction of a Virtual Environment
A virtual environment is create and manage using the venv module. It works with the latest Python version. To establish a virtual environment, choose a location, and run the venv module as handwriting using the directory path. In your command line, enter the following command after typing it.
Python3 -m new-env venv
The above-mentioned command will construct the new-env directory as well as a directory holding a fresh instance of the Python interpreter inside the newly established virtual environment new-env.
The venv folder in the shared directory location houses all virtual environments. The virtual environment must be activate when it has been create by typing the next command.
After entering on Window, press enter.
new-env\Scripts\activate.bat
Run: on UNIX or macOS.
new-env/bin/activate as a source
The shell prompts will change after the virtual environment is activate. create a new virtualenv python indicating which virtual environment we are using.
Package management with pip
Using the Python package manager pip, we may install, upgrade, and uninstall libraries in an active virtual environment. For instance –
New-env) $ pip install requests==2.6.0
Collecting requests==2.6.0
Using cached requests-2.6.0-py2.py3-none-any.whl
Installing collected packages: requests
Successfully installed requests-2.6.0
If we re-run the previous command. It will display the required version is already install. Visit www.arzhost.com for more queries.