1) What is a Virtual Environment in Python
“In Python a virtual environment is an environment in which Python libraries, interpreter and scripts installed are isolated from those installed in other virtual environments. By default any libraries installed are in a virtual environment which is installed as part of your operating system“
2) Why Python needs a Virtual Environment
In Python one of your projects might require a different version of an external library than another one. If you have only one place/ environment to install packages, then you can’t work with two different versions of the same library. This is the most common reasons to use a virtual environment in Python.
The Python virtual environments allow you to install Python packages in an isolated location from the rest of your system instead of installing them system-wide. A virtual environment isolate your project from other projects and system-wide packages. You install packages inside this virtual environment specifically for the project you are working on.
3) How to Create a Virtual Environment
To create a virtual environment for Python we can use pip package or conda. We will use the conda to create Virtual Environment here. Conda is a multi platform and open source package management system. Conda was initially created to solve package management problems for Python data scientists . It is now the one of most popular package manager for Python. To create a virtual environment in conda we To create a virtual environment we will open a anaconda command prompt. and write following command in the command window:
syntax : conda create -n EnvName python=VersionNo(optional)
conda create -n projEnv
conda create -n projEnv python=4
In the above line of code projEnv is the environment name which can be any name, python=4 is the python version which will be used in this environment if no python version is specified then currently installed version of python will be used. The environment will be created in the envs folder where anaconda is installed.
Note: The environment create command will access the internet to check version and download some files from anaconda.com . So you should be connected with internet. If you are in a corporate environment behind a proxy server then you will need to first set the proxy server using commands
In Visual Studio Code
You can also create an environment using Visual Studio Code Terminal window using command:
python -m venv Path_of_Virtual_Environment
e.g: python -m venv D:\MyWork\Python\vEnvs\Py39Env
When we use Visual Studio Code to create Virtual Environment it will create different files and folders on the given path that will be used to activate the environment and run the programs in that environment.
5) How to Activate a Virtual Environment
You can view the already created virtual environments in the Anaconda environment using command as:
conda info --envs
To activate an already created virtual environment in anaconda using command prompt we can use the following command:
conda activate djangoEnv
We can deactivate a Virtual Environment as :
In Visual Studio Code
Whenever we create a Virtual Environment in Visual Studio Code it will create some folders and files that contains configurations, scripts, settings and other files related to that virtual environments. As shown in the command window to create virtual environment. A script file having name activate is also created in the Scripts folder where your environment variable exists. We run this file to activate that virtual environment as.
Note: In windows environment by default windows restrict power shell execution of scripts
On non-Windows computers, the default execution policy is Unrestricted and cannot be changed.
In order to run the above script you need to remove this restriction. In windows we can temporarily allow(Current process) user to execute all script as :
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
The above command will allow the script execution only for the time being this process is available . As soon as you stop or close the process this policy will be cleared. Using the Set-ExecutionPolicy you can also set the policy on permanent basis.
You can view more details on the Set-ExecutionPolicy Commands from the Microsoft Documentation from the url https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-7.3