It's normal for Python designers to introduce and refresh bundles from standard and custom sources to construct applications. The reasons can be anything from utilizing a more up to date rendition of a module to involving a particular variant for relapse.
These requirements and use cases can be difficult to manage when different applications require different versions of the same library. Fortunately, Python provides a solid solution for isolating development environments using virtual environments.
The Python virtual environment is a self-contained directory tree that contains a specific Python installation with all its standard libraries. Applications that require a specific version of the library can be isolated in these virtual environments without contaminating the system or other Python implementations. Each virtual environment is a separate sandbox, so you can create as many virtual environments as you want.
Create and manage virtual environments
To establish virtual conditions in Ubuntu, first introduce the expected reliance bundle by running the order:
$ sudo adept introduce python3-venv
You can now establish virtual conditions by running the order:
$ python3 - m venv myenv
Running the above order will establish a virtual climate named "myenv" in your home registry. To utilize some other variant of Python, you can supplant "python3" above with the full way to the area of the other Python double.
To actuate the custom climate made above, run the order:
$ source myenv/receptacle/enact
At the point when a virtual climate is enacted, the shell brief changes to mirror the virtual climate at present being used. Assuming you run the order "sys.path" in the Python mediator, you will see that the virtual climate is turned out great. Python will presently search for bundles in the custom climate you recently made.
In the event that the shell brief doesn't show the name of the virtual climate as a prefix, any Python record you run will utilize framework Python all things considered. Hence, downloading the virtual climate prior to running the document in the custom environment is essential.
You can deactivate the virtual climate by running the order:
$ deactivate
Each Python virtual environment includes a file "pyvenv.cfg" with the key "include-system-site-packages", which is set to "false" by default. This key tells the virtual environment whether to use system site packages or not. If set to "true", Python in the virtual environment will check system packages in case the package is not found in the virtual environment. This ensures that all packages installed on the system will be imported into the virtual environment.
To delete a virtual environment, just delete the virtual environment directory and it will disappear.
It is possible to create and store virtualenv on external USB drives. You must have a USB drive formatted with the NTFS or EXT file system. The FAT32 file system does not support symlinks, which is required for virtual environments to function.
Using the Pip Package Manager to Install Packages in a Virtual Environment
In a virtual climate, you can utilize the pip bundle supervisor to introduce, overhaul, and uninstall bundles. It is feasible to downsize or refresh a bundle by indicating a particular form number.
You can introduce a bundle utilizing pip by running the order (supplant pygame with the bundle name you need):
$ pip introduce pygame
You can find an accessible pip bundle storehouse here .
To uninstall a bundle, utilize the order:
$ pip uninstall pygame
To see all forms of a bundle, run the order:
$ pip introduce pygame ==
To minimization or move up to a particular rendition, utilize the order (supplant "1.9.5" with the ideal variant number):
$ pip introduce pygame == 1.9.5
To refresh the bundle to the most recent rendition, run the order:
$ pip introduce - update pygame
To see all bundles introduced in a virtual climate, run the order:
$ pip list
To save a rundown of introduced bundles inside virtualenv, run the order:
$ pip freeze > necessities. text
You can utilize the abovementioned "requirements.txt" record for mass establishment of bundles. One use case is to copy a current climate by introducing all bundles without any preparation. To mass introduce pip bundles, run the order:
$ pip introduce - r necessities. text
Using Python Virtual Environments with Non-Python Applications
Applications written in other programming dialects can be sanded in Python virtual conditions insofar as pip or some other Python bundle administrator gives doubles and bundles to those elective programming dialects/structures.
Here is a little guide to make sense of the establishment of Node.js in a Python virtual climate. Show the accompanying orders coordinated:
$ python3 - m venv my_node_env
$ source my_node_env/canister/enact
$ pip introduce circle
$ pip introduce nodeenv
$ nodeenv - p
$ nodeenv - - adaptation
$ hub - - adaptation
When gotten done, you'll have a totally confined Node.js climate. You can establish quite a few virtual conditions for various Node.js applications. Bundles introduced with the npm bundle supervisor will be restricted to the actuated virtual climate as it were.
Creating a virtual Python environment is a great way to containerize development environments. Each virtual environment has its own python binary and its own independent set of packages. Building multiple Python applications on the same system can quickly contaminate your home and home directory, and virtual environments make them easy to maintain.