:::: MENU ::::

Tuesday, November 23, 2021

What is Virtual Environment in python:
"""A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated Python virtual environments for them. This is one of the most important tools that most Python developers use."""
Quoted from geeksforgeeks.org

"""The venv module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from the system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories."""
Quoted from python.org


Table of Contents:
  • Creating a virtual environment
  • Activate the new virtual environment
  • Deactivate the activated environment
  • Creating a virtual environment with the system's default python packages
  • Making requirement.txt files for future use


Creating a virtual environment:
To get started, if the python 3 is not running,; then to get the 
feature of venv, virtualenv tool should be installed by pip package manager.

> pip install virtualenv
But if Python 3 is installed in the system, then you should already have the venv module from the standard library installed.

So, create your new virtual environment inside the directory:
\my_project> python  -m venv "venv_name"    

It will make a directory of your given venv-name with the necessary files and installed packages on this environment are stored in it.

Activate the new virtual environment:

> venv_name\Scripts\activate.bat

Deactivate the activated environment:
> deactivate

Creating a virtual environment with the system's default python packages:
Sometimes we want to create a virtual environment with 
installation of system packages automatically.

Here is the command to create the environment:
my_project> python -m venv myvenv --system-site-packages
if we want to see the only local installed packages, we can use this command:
> pip list --local

Making requirement.txt files for future use:
\my_project> pip freeze
it will output all installed packages of the currently activated virtual environment. Copy the package list from Command-prompt, then make a requirement.txt file and paste it.
\my_project> pip install -r requirement.txt

\my_project> python -m venv myvenv --system-site-package
it will create a virtual environment named "myvenv" with the system's default python packages.





0 comments:

Post a Comment