knowledge-kitchen
/
course-notes
class: center, middle # Virtual Environments in Python Isolating and documenting project dependencies. --- name: agenda # Agenda 1. [Overview](#overview) 1. [pip](#pip) 1. [pipenv](#pipenv) 1. [venv](#venv) 1. [Using IDEs with virtual environments](#ide) 1. [Conclusions](#conclusions) --- name: overview # Overview -- ## Concept Python comes with an ability to create _virtual environments_ - specific areas of memory and disk space where you can install the dependencies and settings necessary to run a specific app in isolation from other apps on the same computer. -- - a virtual environment is a **sandboxed areas of memory where modules can be installed** without affecting the modules installed and available in other projects. -- - **each project can have its own isolated environment** with its own set of dependencies, which may be different from that of other projects. -- - the configuration of a given virtual environment can be saved into a simple file that can be shared; this **allows multiple developers to set up the same environment on different machines**. -- - when developing an application that depends upon a specific set of modules, **it is good practice to install dependencies into a virtual environment**. --- template: overview ## Choices There are multiple tools for setting Python virtual environments. We will focus on: -- - [`venv`](https://docs.python.org/3/library/venv.html) - virtual environment manager -- - [`pipenv`](https://pypi.org/project/pipenv/) - a wrapper around `venv` and [`pip`](#pip) so you can use both through one interface -- Another popular tool for the job is [`conda`](https://docs.conda.io/en/latest/), but we will not discuss it here as it is most commonly used with the [Anaconda](https://www.anaconda.com/) distribution of Python, not the _standard_ distribution. --- name: pip # pip -- ## Overview Before we discuss virtual environments, we must understand [`pip`](https://pypi.org/project/pip/), the standard "_package manager_" used to download and install dependencies into a Python project. -- - `pip` comes with the standard distribution of Python... -- - on some systems, the command to run it is `pip`, on others it is `pip3`... if one doesn't work, try the other. We use `pip` in the examples below. -- - The `pip install` command is used to install a module into the current Python environment, e.g. `pip install Flask` or `pip install pymongo` -- - By default, `pip` downloads modules from the [Python Package Index](https://pypi.org/) (PyPi), but it can also install modules from other sources, such as [`git` repositories](https://pip.pypa.io/en/stable/topics/vcs-support/). --- template: pip ## Base environment If no virtual environment is active, `pip` will install modules into the global _base_ Python environment, which is shared by all projects on the computer. -- - However, if a virtual environment is active, dependencies installed with `pip` will be placed into the virtual environment for the current project, rather than the base environment. --- template: pip name: pip-freeze ## Documenting dependencies The `pip freeze` command is used to list all of the modules installed into the current Python environment. -- - To save the list of dependencies into a file commonly named `requirements.txt`, run: `pip freeze > requirements.txt` -- - This file can be shared and used to re-install the same set of dependencies into a different computer or a different Python environment, e.g. `pip install -r requirements.txt` --- name: pipenv # pipenv -- ## Overview The ability to make virtual environments with [pipenv](https://pypi.org/project/pipenv/) is relatively easy, but it does not come pre-installed with Python. It must be installed. -- Install `pipenv` using `pip`: ```bash pip install pipenv ``` -- Activate it: ```bash pipenv shell ``` Your command line is **now running within a virtual environment**. Installing or uninstalling any modules will only affect the current environment. --- template: pipenv ## Path to environment Generally, you don't need to worry about where `pipenv` stores the virtual environment files... it just works. However, in some cases, such as [setting an IDE](#ide) to use a specific environment, it may be helpful to know the path to the virtual environment files. When running `pipenv shell`, the path to the virtual environment is printed to the console. ![pipenv shell file path](../assets/virtual-environments/pipenv-shell-file-path.png) --- template: pipenv ## Installing dependencies Use the `pipenv install` command to download and install dependencies into the current virtual environment. ```bash pipenv install Flask ``` ```bash pipenv install pymongo ``` -- As you install dependencies with `pipenv`, you will a file named `Pipfile` that is automatically created (if it doesn't yet exist) and modified to list the dependencies you've installed. --- template: pipenv You may want to specify a specific version of Python to be used in this virtual environment: -- For example: ```bash pipenv --python 3.10 ``` --- template: pipenv ## Configuration file The file named `Pipfile` contains the list of dependencies for the current project. - dependencies are the other Python modules that this app depends upon to run. -- - the `Pipfile` lists each dependency and the version number that should be installed. -- - the `Pipefile` includes the version of Python, if any, that it has been specified for the virtual environment. -- - if your project already had a file named `Pipenv` when you activated the virtual environment, all dependencies listed therein would have been automatically installed. -- - more about [`pipenv`](https://realpython.com/pipenv-guide/#pipenv-introduction) --- template: pipenv ## Naming virtual environments If you have many virtual environments for many projects, it can be helpful to name them intuitive mnemonic names. In `pipenv`, we do this by _first_ creating an environment variable in the command shell (Terminal) named `PIPENV_CUSTOM_VENV_NAME` and setting it to our desired name. ```bash export PIPENV_CUSTOM_VENV_NAME=my_custom_venv_name ``` _Secondly_, xwe create a new virtual environment which adopts that name: ```bash pipenv shell ``` --- name: venv # venv -- ## Overview If you are unable or refuse to use `pipenv` for some reason, you can use the more traditional [venv](https://docs.python.org/3/library/venv.html) instead. The ability to make virtual environments with `venv` comes included with standard Python distributions. -- This command creates a new virtual environment with the name `.venv`: ```bash python -m venv .venv # try 'python3' instead of 'python' if your system requires it ``` -- To activate the virtual environment named `.venv`... On Mac: ```bash source .venv/bin/activate ``` On Windows: ```bash .venv\Scripts\activate.bat ``` --- template: venv ## Installing dependencies Once a `venv` virtual environment has been activated, any dependencies installed with `pip` will be installed into the virtual environment, not the global environment. ```bash pip install Flask ``` ```bash pip install pymongo ``` -- As [with `pip` generally](#pip-freeze), you can run `pip freeze` to save the installed dependencies into a settings file named `requirements.txt`. This file can then be shared and used to install the same dependencies on other computers or in other virtual environments. --- name: ide # Using IDEs with virtual environments -- ## Overview When you have set a virtual environment using the command shell (i.e. terminal), the IDE you are using to edit the code may not be configured to use the same environment. Any good Python IDE will allow you to select the virtual environment to use when running the code. --- template: ide ## Visual Studio Code For example, if you are using [Visual Studio Code](https://code.visualstudio.com/), you may notice that the [linter](../software-testing#code-linting) in the IDE will flag code as incorrect that references modules that are indeed installed in the virtual environment. This is usually because the IDE is not set to use the same environment. -- - To fix this, configure the IDE to use the same environment as the command shell. This can be done by selecting `Python: Select Interpreter` from the [Command Palette](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette) and choosing the interpreter marked with the virtual environment you have set up. - If you don't see your desired environment in the list, you may enter the path to it manually. ![VSCode select Python interpreter](../assets/virtual-environments/vscode-select-python-interpreter.png) --- name: conclusions # Conclusions Thank you. Bye