Monday, April 21, 2025

How to Create Python Virtual Environment - venv Tutorial

You are strongly advised to check corresponding YouTube video at the end of this article.

Using a virtual environment, like venv in Python, is a smart idea for a few reasons:

  1. Isolation: Virtual environments allow you to create an isolated environment for your Python project, separate from the system's Python installation and other projects. This isolation prevents conflicts between different project dependencies and ensures that you have a clean and consistent environment for your project.

  2. Reproducibility: Virtual environments allow you to specify the exact Python version and package versions required for your project. This makes it easier to reproduce your project's environment on another machine or to deploy your project to a production environment.

  3. Security: Using virtual environments can help prevent security vulnerabilities. Since your project's dependencies are installed in an isolated environment, any vulnerabilities or security issues with those dependencies won't affect other projects or the system as a whole.

Using a virtual environment provides a clean and consistent environment for your Python project, with all the required dependencies, while avoiding potential conflicts and security issues. 

C:\>python -m venv test_stuff

C:\>cd test_stuff

C:\test_stuff>dir
 Volume in drive C is New Volume
 Volume Serial Number is B082-0A47

 Directory of C:\test_stuff

06/21/2020  09:08 PM              .
06/21/2020  09:08 PM              ..
06/21/2020  09:08 PM              Include
06/21/2020  09:08 PM              Lib
06/21/2020  09:08 PM                78 pyvenv.cfg
06/21/2020  09:09 PM              Scripts
               1 File(s)             78 bytes
               5 Dir(s)  78,625,415,168 bytes free

C:\test_stuff>

This is a sequence of commands that create a virtual environment in Python:

  • python -m venv test_stuff: This command creates a virtual environment named "test_stuff" using the built-in venv module of Python. The -m flag tells Python to run a module as a script, and venv is the name of the module that creates virtual environments.

  • cd test_stuff: This command changes the current working directory to the newly created virtual environment.

  • dir: This command lists the contents of the current working directory. Since we're now in the virtual environment directory, we see the folders and files that make up the virtual environment.

The Include and Lib directories are where the standard library and other installed packages are stored. The pyvenv.cfg file contains configuration options for the virtual environment, and the Scripts directory contains scripts for activating and deactivating the virtual environment, as well as running Python scripts within the environment. 


C:\test_stuff>cd Scripts

C:\test_stuff\Scripts>activate

(test_stuff) C:\test_stuff\Scripts>

The activate script is used to activate the virtual environment.

Once we run the activate script, we will notice that the name of the virtual environment is added to the command prompt, indicating that the virtual environment is active. In our example, the virtual environment named test_stuff is active and the command prompt shows (test_stuff) at the beginning of the prompt. Any packages installed while the virtual environment is active will be installed only in that virtual environment and will not affect the global Python environment or other virtual environments. 

(test_stuff) C:\test_stuff\Scripts>python -m pip install --upgrade pip
Collecting pip
  Using cached https://files.pythonhosted.org/packages/.....
Installing collected packages: pip
  Found existing installation: pip 19.2.3
    Uninstalling pip-19.2.3:
      Successfully uninstalled pip-19.2.3
Successfully installed pip-20.1.1

(test_stuff) C:\test_stuff\Scripts>

This command upgrades the "pip" package in the virtual environment to the latest version using pip itself. The output shows that pip was successfully upgraded from version 19.2.3 to version 20.1.1. 

(test_stuff) C:\test_stuff\Scripts>pip install django
Collecting django
  Downloading Django-3.0.7-py3-none-any.whl (7.5 MB)
     |████████████████████████████████| 7.5 MB 97 kB/s
Collecting pytz
  Downloading pytz-2020.1-py2.py3-none-any.whl (510 kB)
     |████████████████████████████████| 510 kB ...
Collecting asgiref~=3.2
  Downloading asgiref-3.2.10-py3-none-any.whl (19 kB)
Collecting sqlparse>=0.2.2
  Downloading sqlparse-0.3.1-py2.py3-none-any.whl (40 kB)
     |████████████████████████████████| 40 kB 427 kB/s
Installing collected packages: pytz, asgiref, sqlparse, django
Successfully installed asgiref-3.2.10 django-3.0.7 pytz-2020.1 sqlparse-0.3.1

(test_stuff) C:\test_stuff\Scripts>

This code installs the Django web framework using pip. Pip installs all the dependencies required by Django such as pytz, asgiref, and sqlparse. Once the installation is complete, the Django web framework is now ready to be used in the virtual environment. 


(test_stuff) C:\test_stuff\Scripts>deactivate
C:\test_stuff\Scripts>

This code deactivates the virtual environment created earlier using the command "python -m venv test_stuff" and returns the command prompt to the default system environment. The virtual environment is indicated by the prefix "(test_stuff)" in the command prompt.

The "deactivate" command is a built-in command provided by the virtual environment package in Python. When this command is run, it disables the virtual environment and restores the default system environment.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...