19  Module 3 - Day 2

Topics

19.1 Virtual Environment

Many times it happens that different projects have requirements of python packages such that they conflict each other. In such cases how do you work on two different project on same machine? If we install python packages for one project, those packages will confict with other projects. Virtual environment is there to help us. Virtual environment allows us to have set of python packages seperately for each project. Also added advantage is, it won’t affect system python’s packages. The way to handle this is with help of venv module we create virtual environment for each project. All requirements for the proejct are installed in the virtual environment and not in system python’s packages. Let’s take some examples.

19.1.1 Conflicting Requirements

Suppose we have two projects, datascraping and analytics. For datascraping project requirements are following packages::

requests==2.24.0 openpyxl==2.4.8

and analytics project needs following packages::

pandas==1.1.2 openpyxl==3.0.5 requests==2.24.0

Now here is confiliting requirement, one project needs openpyxl verson 2.4.8 and other needs 3.0.5.

19.1.2 creating env

To create virutal environment on your system, what you need is python version > 3.5. Python comes with a package called venv (virtual environment). For older python, virtualenv was seperate application. We are going to work with virtual environment that comes with python 3. Easy steps to work with it are as given below. Open up terminal on linux/mac or cmd terminal on windows. on the prompt type following command to create virtual environment with name env1::

  python -m venv env1

This will create a folder with name env1 in the current directory. On linux it will have following contents::

  +-env1
    |
    +-bin
    +-include
    +-lib
    +-lib64
    +-pyenv.cfg

on windows system it will have following contents::

  +-env1
    |
    +-Include
    +-Lib
    +-Scripts
    +-pyenv.cfg

To activate virtual environment on linux run following command on terminal.::

  bash$ source env1/bin/activate
  (env1) bash$ # you can see the env1 environment activated as change in prompt

To activate virtual environment on windows run following command on windows cmd terminal::

  C:\Users\vik> env1\bin\activate.bat
  (env1) C:\Users\vik>

19.1.3 Installing packages in virtual environment

Once the virtul environment is created and activated, we are ready to use it. To install packages in this active virtual environment use pip install::

  pip install typer
  Collecting typer
    Using cached https://files.pythonhosted.org/packages/90/34/d138832f6945432c638f32137e6c79a3b682f06a63c488dcfaca6b166c64/typer-0.3.2-py3-none-any.whl
  Collecting click<7.2.0,>=7.1.1 (from typer)
    Using cached https://files.pythonhosted.org/packages/d2/3d/fa76db83bf75c4f8d338c2fd15c8d33fdd7ad23a9b5e57eb6c5de26b430e/click-7.1.2-py2.py3-none-any.whl
  Installing collected packages: click, typer
  Successfully installed click-7.1.2 typer-0.3.2

to check packages installed ::

  pip list
  Package    Version
  ---------- -------
  click      7.1.2
  pip        19.2.3
  setuptools 41.2.0
  typer      0.3.2

19.1.4 requirements.txt

If we want to replicate exact same virtual environment on other machine we need list of packages that pip can understand. The format is called as requirements file. it can be generated using::

  pip freeze
  click==7.1.2
  typer==0.3.2

The output can be saved to a file with name requirements.txt. This file can be used in other virtual env to recreate the same environment. For example, lets make use of above requirements to recreate another environment with name env1copy::

  bash$ python -m venv env1copy
  bash$ source env1copy/bin/activate
  (env1copy) bash$ pip install -r requirements.txt
  Collecting click==7.1.2 (from -r env1/requirements.txt (line 1))
    Using cached https://files.pythonhosted.org/packages/d2/3d/fa76db83bf75c4f8d338c2fd15c8d33fdd7ad23a9b5e57eb6c5de26b430e/click-7.1.2-py2.py3-none-any.whl
  Collecting typer==0.3.2 (from -r env1/requirements.txt (line 2))
    Using cached https://files.pythonhosted.org/packages/90/34/d138832f6945432c638f32137e6c79a3b682f06a63c488dcfaca6b166c64/typer-0.3.2-py3-none-any.whl
  Installing collected packages: click, typer
  Successfully installed click-7.1.2 typer-0.3.2

you can check the packages installed::

  pip freeze
  click==7.1.2
  typer==0.3.2

19.1.5 Summary

  1. Virtual environment can be created by any user. No admin privileges required.
  2. Every virtual environment is stored in seperate folder.
  3. Packages installed in a virtual environment are only in that particular virtual environment.
  4. With requirements.txt it is very easy to recreate the same replica of a particular virtul environment.
!pip install jupyterlab pandas xlsxwriter lxml requests typer
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: jupyterlab in /opt/tljh/user/lib/python3.12/site-packages (4.5.0)
Requirement already satisfied: pandas in /opt/tljh/user/lib/python3.12/site-packages (2.3.3)
Requirement already satisfied: xlsxwriter in /opt/tljh/user/lib/python3.12/site-packages (3.2.9)
Requirement already satisfied: lxml in /opt/tljh/user/lib/python3.12/site-packages (6.0.2)
Requirement already satisfied: requests in /opt/tljh/user/lib/python3.12/site-packages (2.32.3)
Requirement already satisfied: typer in /home/jupyter-vikrant/.local/lib/python3.12/site-packages (0.20.0)
Requirement already satisfied: async-lru>=1.0.0 in /opt/tljh/user/lib/python3.12/site-packages (from jupyterlab) (2.0.5)
Requirement already satisfied: httpx<1,>=0.25.0 in /opt/tljh/user/lib/python3.12/site-packages (from jupyterlab) (0.28.1)
Requirement already satisfied: ipykernel!=6.30.0,>=6.5.0 in /opt/tljh/user/lib/python3.12/site-packages (from jupyterlab) (7.1.0)
Requirement already satisfied: jinja2>=3.0.3 in /opt/tljh/user/lib/python3.12/site-packages (from jupyterlab) (3.1.6)
Requirement already satisfied: jupyter-core in /opt/tljh/user/lib/python3.12/site-packages (from jupyterlab) (5.9.1)
Requirement already satisfied: jupyter-lsp>=2.0.0 in /opt/tljh/user/lib/python3.12/site-packages (from jupyterlab) (2.3.0)
Requirement already satisfied: jupyter-server<3,>=2.4.0 in /opt/tljh/user/lib/python3.12/site-packages (from jupyterlab) (2.17.0)
Requirement already satisfied: jupyterlab-server<3,>=2.28.0 in /opt/tljh/user/lib/python3.12/site-packages (from jupyterlab) (2.28.0)
Requirement already satisfied: notebook-shim>=0.2 in /opt/tljh/user/lib/python3.12/site-packages (from jupyterlab) (0.2.4)
Requirement already satisfied: packaging in /opt/tljh/user/lib/python3.12/site-packages (from jupyterlab) (24.1)
Requirement already satisfied: setuptools>=41.1.0 in /opt/tljh/user/lib/python3.12/site-packages (from jupyterlab) (74.1.2)
Requirement already satisfied: tornado>=6.2.0 in /opt/tljh/user/lib/python3.12/site-packages (from jupyterlab) (6.5.2)
Requirement already satisfied: traitlets in /opt/tljh/user/lib/python3.12/site-packages (from jupyterlab) (5.14.3)
Requirement already satisfied: numpy>=1.26.0 in /opt/tljh/user/lib/python3.12/site-packages (from pandas) (2.3.5)
Requirement already satisfied: python-dateutil>=2.8.2 in /opt/tljh/user/lib/python3.12/site-packages (from pandas) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in /opt/tljh/user/lib/python3.12/site-packages (from pandas) (2025.2)
Requirement already satisfied: tzdata>=2022.7 in /opt/tljh/user/lib/python3.12/site-packages (from pandas) (2025.2)
Requirement already satisfied: charset-normalizer<4,>=2 in /opt/tljh/user/lib/python3.12/site-packages (from requests) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /opt/tljh/user/lib/python3.12/site-packages (from requests) (3.10)
Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/tljh/user/lib/python3.12/site-packages (from requests) (2.2.3)
Requirement already satisfied: certifi>=2017.4.17 in /opt/tljh/user/lib/python3.12/site-packages (from requests) (2025.11.12)
Requirement already satisfied: click>=8.0.0 in /home/jupyter-vikrant/.local/lib/python3.12/site-packages (from typer) (8.3.1)
Requirement already satisfied: typing-extensions>=3.7.4.3 in /opt/tljh/user/lib/python3.12/site-packages (from typer) (4.15.0)
Requirement already satisfied: shellingham>=1.3.0 in /home/jupyter-vikrant/.local/lib/python3.12/site-packages (from typer) (1.5.4)
Requirement already satisfied: rich>=10.11.0 in /home/jupyter-vikrant/.local/lib/python3.12/site-packages (from typer) (14.2.0)
Requirement already satisfied: anyio in /opt/tljh/user/lib/python3.12/site-packages (from httpx<1,>=0.25.0->jupyterlab) (4.12.0)
Requirement already satisfied: httpcore==1.* in /opt/tljh/user/lib/python3.12/site-packages (from httpx<1,>=0.25.0->jupyterlab) (1.0.9)
Requirement already satisfied: h11>=0.16 in /opt/tljh/user/lib/python3.12/site-packages (from httpcore==1.*->httpx<1,>=0.25.0->jupyterlab) (0.16.0)
Requirement already satisfied: comm>=0.1.1 in /opt/tljh/user/lib/python3.12/site-packages (from ipykernel!=6.30.0,>=6.5.0->jupyterlab) (0.2.3)
Requirement already satisfied: debugpy>=1.6.5 in /opt/tljh/user/lib/python3.12/site-packages (from ipykernel!=6.30.0,>=6.5.0->jupyterlab) (1.8.17)
Requirement already satisfied: ipython>=7.23.1 in /opt/tljh/user/lib/python3.12/site-packages (from ipykernel!=6.30.0,>=6.5.0->jupyterlab) (9.8.0)
Requirement already satisfied: jupyter-client>=8.0.0 in /opt/tljh/user/lib/python3.12/site-packages (from ipykernel!=6.30.0,>=6.5.0->jupyterlab) (8.6.3)
Requirement already satisfied: matplotlib-inline>=0.1 in /opt/tljh/user/lib/python3.12/site-packages (from ipykernel!=6.30.0,>=6.5.0->jupyterlab) (0.2.1)
Requirement already satisfied: nest-asyncio>=1.4 in /opt/tljh/user/lib/python3.12/site-packages (from ipykernel!=6.30.0,>=6.5.0->jupyterlab) (1.6.0)
Requirement already satisfied: psutil>=5.7 in /opt/tljh/user/lib/python3.12/site-packages (from ipykernel!=6.30.0,>=6.5.0->jupyterlab) (7.1.3)
Requirement already satisfied: pyzmq>=25 in /opt/tljh/user/lib/python3.12/site-packages (from ipykernel!=6.30.0,>=6.5.0->jupyterlab) (27.1.0)
Requirement already satisfied: MarkupSafe>=2.0 in /opt/tljh/user/lib/python3.12/site-packages (from jinja2>=3.0.3->jupyterlab) (3.0.3)
Requirement already satisfied: platformdirs>=2.5 in /opt/tljh/user/lib/python3.12/site-packages (from jupyter-core->jupyterlab) (4.3.6)
Requirement already satisfied: argon2-cffi>=21.1 in /opt/tljh/user/lib/python3.12/site-packages (from jupyter-server<3,>=2.4.0->jupyterlab) (25.1.0)
Requirement already satisfied: jupyter-events>=0.11.0 in /opt/tljh/user/lib/python3.12/site-packages (from jupyter-server<3,>=2.4.0->jupyterlab) (0.12.0)
Requirement already satisfied: jupyter-server-terminals>=0.4.4 in /opt/tljh/user/lib/python3.12/site-packages (from jupyter-server<3,>=2.4.0->jupyterlab) (0.5.3)
Requirement already satisfied: nbconvert>=6.4.4 in /opt/tljh/user/lib/python3.12/site-packages (from jupyter-server<3,>=2.4.0->jupyterlab) (7.16.6)
Requirement already satisfied: nbformat>=5.3.0 in /opt/tljh/user/lib/python3.12/site-packages (from jupyter-server<3,>=2.4.0->jupyterlab) (5.10.4)
Requirement already satisfied: prometheus-client>=0.9 in /opt/tljh/user/lib/python3.12/site-packages (from jupyter-server<3,>=2.4.0->jupyterlab) (0.23.1)
Requirement already satisfied: send2trash>=1.8.2 in /opt/tljh/user/lib/python3.12/site-packages (from jupyter-server<3,>=2.4.0->jupyterlab) (1.8.3)
Requirement already satisfied: terminado>=0.8.3 in /opt/tljh/user/lib/python3.12/site-packages (from jupyter-server<3,>=2.4.0->jupyterlab) (0.18.1)
Requirement already satisfied: websocket-client>=1.7 in /opt/tljh/user/lib/python3.12/site-packages (from jupyter-server<3,>=2.4.0->jupyterlab) (1.9.0)
Requirement already satisfied: babel>=2.10 in /opt/tljh/user/lib/python3.12/site-packages (from jupyterlab-server<3,>=2.28.0->jupyterlab) (2.17.0)
Requirement already satisfied: json5>=0.9.0 in /opt/tljh/user/lib/python3.12/site-packages (from jupyterlab-server<3,>=2.28.0->jupyterlab) (0.12.1)
Requirement already satisfied: jsonschema>=4.18.0 in /opt/tljh/user/lib/python3.12/site-packages (from jupyterlab-server<3,>=2.28.0->jupyterlab) (4.25.1)
Requirement already satisfied: six>=1.5 in /opt/tljh/user/lib/python3.12/site-packages (from python-dateutil>=2.8.2->pandas) (1.17.0)
Requirement already satisfied: markdown-it-py>=2.2.0 in /home/jupyter-vikrant/.local/lib/python3.12/site-packages (from rich>=10.11.0->typer) (4.0.0)
Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /opt/tljh/user/lib/python3.12/site-packages (from rich>=10.11.0->typer) (2.19.2)
Requirement already satisfied: argon2-cffi-bindings in /opt/tljh/user/lib/python3.12/site-packages (from argon2-cffi>=21.1->jupyter-server<3,>=2.4.0->jupyterlab) (25.1.0)
Requirement already satisfied: decorator>=4.3.2 in /opt/tljh/user/lib/python3.12/site-packages (from ipython>=7.23.1->ipykernel!=6.30.0,>=6.5.0->jupyterlab) (5.2.1)
Requirement already satisfied: ipython-pygments-lexers>=1.0.0 in /opt/tljh/user/lib/python3.12/site-packages (from ipython>=7.23.1->ipykernel!=6.30.0,>=6.5.0->jupyterlab) (1.1.1)
Requirement already satisfied: jedi>=0.18.1 in /opt/tljh/user/lib/python3.12/site-packages (from ipython>=7.23.1->ipykernel!=6.30.0,>=6.5.0->jupyterlab) (0.19.2)
Requirement already satisfied: pexpect>4.3 in /opt/tljh/user/lib/python3.12/site-packages (from ipython>=7.23.1->ipykernel!=6.30.0,>=6.5.0->jupyterlab) (4.9.0)
Requirement already satisfied: prompt_toolkit<3.1.0,>=3.0.41 in /opt/tljh/user/lib/python3.12/site-packages (from ipython>=7.23.1->ipykernel!=6.30.0,>=6.5.0->jupyterlab) (3.0.52)
Requirement already satisfied: stack_data>=0.6.0 in /opt/tljh/user/lib/python3.12/site-packages (from ipython>=7.23.1->ipykernel!=6.30.0,>=6.5.0->jupyterlab) (0.6.3)
Requirement already satisfied: attrs>=22.2.0 in /opt/tljh/user/lib/python3.12/site-packages (from jsonschema>=4.18.0->jupyterlab-server<3,>=2.28.0->jupyterlab) (25.4.0)
Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /opt/tljh/user/lib/python3.12/site-packages (from jsonschema>=4.18.0->jupyterlab-server<3,>=2.28.0->jupyterlab) (2025.9.1)
Requirement already satisfied: referencing>=0.28.4 in /opt/tljh/user/lib/python3.12/site-packages (from jsonschema>=4.18.0->jupyterlab-server<3,>=2.28.0->jupyterlab) (0.37.0)
Requirement already satisfied: rpds-py>=0.7.1 in /opt/tljh/user/lib/python3.12/site-packages (from jsonschema>=4.18.0->jupyterlab-server<3,>=2.28.0->jupyterlab) (0.30.0)
Requirement already satisfied: python-json-logger>=2.0.4 in /opt/tljh/user/lib/python3.12/site-packages (from jupyter-events>=0.11.0->jupyter-server<3,>=2.4.0->jupyterlab) (4.0.0)
Requirement already satisfied: pyyaml>=5.3 in /opt/tljh/user/lib/python3.12/site-packages (from jupyter-events>=0.11.0->jupyter-server<3,>=2.4.0->jupyterlab) (6.0.3)
Requirement already satisfied: rfc3339-validator in /opt/tljh/user/lib/python3.12/site-packages (from jupyter-events>=0.11.0->jupyter-server<3,>=2.4.0->jupyterlab) (0.1.4)
Requirement already satisfied: rfc3986-validator>=0.1.1 in /opt/tljh/user/lib/python3.12/site-packages (from jupyter-events>=0.11.0->jupyter-server<3,>=2.4.0->jupyterlab) (0.1.1)
Requirement already satisfied: mdurl~=0.1 in /home/jupyter-vikrant/.local/lib/python3.12/site-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer) (0.1.2)
Requirement already satisfied: beautifulsoup4 in /opt/tljh/user/lib/python3.12/site-packages (from nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->jupyterlab) (4.14.3)
Requirement already satisfied: bleach!=5.0.0 in /opt/tljh/user/lib/python3.12/site-packages (from bleach[css]!=5.0.0->nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->jupyterlab) (6.3.0)
Requirement already satisfied: defusedxml in /opt/tljh/user/lib/python3.12/site-packages (from nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->jupyterlab) (0.7.1)
Requirement already satisfied: jupyterlab-pygments in /opt/tljh/user/lib/python3.12/site-packages (from nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->jupyterlab) (0.3.0)
Requirement already satisfied: mistune<4,>=2.0.3 in /opt/tljh/user/lib/python3.12/site-packages (from nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->jupyterlab) (3.1.4)
Requirement already satisfied: nbclient>=0.5.0 in /opt/tljh/user/lib/python3.12/site-packages (from nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->jupyterlab) (0.10.2)
Requirement already satisfied: pandocfilters>=1.4.1 in /opt/tljh/user/lib/python3.12/site-packages (from nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->jupyterlab) (1.5.1)
Requirement already satisfied: fastjsonschema>=2.15 in /opt/tljh/user/lib/python3.12/site-packages (from nbformat>=5.3.0->jupyter-server<3,>=2.4.0->jupyterlab) (2.21.2)
Requirement already satisfied: ptyprocess in /opt/tljh/user/lib/python3.12/site-packages (from terminado>=0.8.3->jupyter-server<3,>=2.4.0->jupyterlab) (0.7.0)
Requirement already satisfied: webencodings in /opt/tljh/user/lib/python3.12/site-packages (from bleach!=5.0.0->bleach[css]!=5.0.0->nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->jupyterlab) (0.5.1)
Requirement already satisfied: tinycss2<1.5,>=1.1.0 in /opt/tljh/user/lib/python3.12/site-packages (from bleach[css]!=5.0.0->nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->jupyterlab) (1.4.0)
Requirement already satisfied: parso<0.9.0,>=0.8.4 in /opt/tljh/user/lib/python3.12/site-packages (from jedi>=0.18.1->ipython>=7.23.1->ipykernel!=6.30.0,>=6.5.0->jupyterlab) (0.8.5)
Requirement already satisfied: fqdn in /opt/tljh/user/lib/python3.12/site-packages (from jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.11.0->jupyter-server<3,>=2.4.0->jupyterlab) (1.5.1)
Requirement already satisfied: isoduration in /opt/tljh/user/lib/python3.12/site-packages (from jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.11.0->jupyter-server<3,>=2.4.0->jupyterlab) (20.11.0)
Requirement already satisfied: jsonpointer>1.13 in /opt/tljh/user/lib/python3.12/site-packages (from jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.11.0->jupyter-server<3,>=2.4.0->jupyterlab) (3.0.0)
Requirement already satisfied: rfc3987-syntax>=1.1.0 in /opt/tljh/user/lib/python3.12/site-packages (from jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.11.0->jupyter-server<3,>=2.4.0->jupyterlab) (1.1.0)
Requirement already satisfied: uri-template in /opt/tljh/user/lib/python3.12/site-packages (from jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.11.0->jupyter-server<3,>=2.4.0->jupyterlab) (1.3.0)
Requirement already satisfied: webcolors>=24.6.0 in /opt/tljh/user/lib/python3.12/site-packages (from jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.11.0->jupyter-server<3,>=2.4.0->jupyterlab) (25.10.0)
Requirement already satisfied: wcwidth in /opt/tljh/user/lib/python3.12/site-packages (from prompt_toolkit<3.1.0,>=3.0.41->ipython>=7.23.1->ipykernel!=6.30.0,>=6.5.0->jupyterlab) (0.2.14)
Requirement already satisfied: executing>=1.2.0 in /opt/tljh/user/lib/python3.12/site-packages (from stack_data>=0.6.0->ipython>=7.23.1->ipykernel!=6.30.0,>=6.5.0->jupyterlab) (2.2.1)
Requirement already satisfied: asttokens>=2.1.0 in /opt/tljh/user/lib/python3.12/site-packages (from stack_data>=0.6.0->ipython>=7.23.1->ipykernel!=6.30.0,>=6.5.0->jupyterlab) (3.0.1)
Requirement already satisfied: pure-eval in /opt/tljh/user/lib/python3.12/site-packages (from stack_data>=0.6.0->ipython>=7.23.1->ipykernel!=6.30.0,>=6.5.0->jupyterlab) (0.2.3)
Requirement already satisfied: cffi>=1.0.1 in /opt/tljh/user/lib/python3.12/site-packages (from argon2-cffi-bindings->argon2-cffi>=21.1->jupyter-server<3,>=2.4.0->jupyterlab) (2.0.0)
Requirement already satisfied: soupsieve>=1.6.1 in /opt/tljh/user/lib/python3.12/site-packages (from beautifulsoup4->nbconvert>=6.4.4->jupyter-server<3,>=2.4.0->jupyterlab) (2.8)
Requirement already satisfied: pycparser in /opt/tljh/user/lib/python3.12/site-packages (from cffi>=1.0.1->argon2-cffi-bindings->argon2-cffi>=21.1->jupyter-server<3,>=2.4.0->jupyterlab) (2.22)
Requirement already satisfied: lark>=1.2.2 in /opt/tljh/user/lib/python3.12/site-packages (from rfc3987-syntax>=1.1.0->jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.11.0->jupyter-server<3,>=2.4.0->jupyterlab) (1.3.1)
Requirement already satisfied: arrow>=0.15.0 in /opt/tljh/user/lib/python3.12/site-packages (from isoduration->jsonschema[format-nongpl]>=4.18.0->jupyter-events>=0.11.0->jupyter-server<3,>=2.4.0->jupyterlab) (1.4.0)