Automate python 3 virtual environment in windows OS

Automate python 3 virtual environment in windows OS

Setup, activate, deactivate and delete python 3 virtual environment in windows OS using batch scripts

Python virtual environment

Python applications will often use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the application may require that a particular bug has been fixed or the application may be written using an obsolete version of the library’s interface.

This means it may not be possible for one Python installation to meet the requirements of every application. The solution for this problem is to create a virtual environment, a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages. Different applications can then use different virtual environments to satisfy their required version of libraries.

Note: Please download and install python 3 in your system before moving into next section. You can follow this video to install python 3.

Note: Please make sure python 3 is accessible from command line using py command.

Setup virtual environment

We can create virtual environment inside any folder. Copy the below code in a file called setupVenv.bat and save it in the folder where you want to create a virtual environment folder.

@ECHO OFF
@REM  It creates virtual environment with system packages

py -m venv %~dp0\venv
CALL activateVenv.bat
CALL updatePythonLibraries.bat

The above batch script will create a venv folder inside current folder and subfolders inside it, containing a copy of the Python interpreter and various supporting files.

Activate virtual environment

We need to activate virtual environment in command line before using it. Copy below code inside activateVenv.bat and save it in parent folder of setupVenv.bat.

@ECHO OFF
@REM activates virtual env from any location
@REM It takes absolute directory path of this batch file and activates virtual env

if exist %~dp0\venv\Scripts\activate.bat (
    CALL %~dp0\venv\Scripts\activate.bat
) else (
    printf "virtual environment not found"
)

If virtual environment activates successfully then we will find (venv) in starting of command prompt.

venv.PNG

Deactivate virtual environment

We can deactivate virtual environment from below code. Copy below code inside deactivateVenv.bat and save it in same folder where we saved setupVenv.bat and activateVenv.bat.

@ECHO OFF
if defined VIRTUAL_ENV (deactivate)
if not defined VIRTUAL_ENV (printf "virtual environment is not active")

After virtual environment gets deactivated, (venv) will be removed from starting of command prompt.

venvRemoved.PNG

Delete virtual environment

We can delete virtual environment if we don't need it. Copy below code in deleteVenv.bat and save it in same folder where we saved above three files.

@ECHO OFF
if defined VIRTUAL_ENV (CALL %~dp0\deactivateVenv.bat)
rmdir venv /s /Q

This command will first deactivate virtual environment if active already and delete venv folder.

Summary

  • setupVenv.bat: creates virtual environment (venv folder) in same folder where batch file is present.
  • activateVenv.bat: It activates virtual environment in your commandline.
  • deactivateVenv.bat: It deactivates virtual environment in your commandline.
  • deleteVenv.bat: It deletes venv folder which contained all required files to activate virtual environment.