Overview
Questions
What is a virtual environment?
How do virtual environments help manage dependencies?
How do you create and activate a virtual environment?
Objectives
Learn about virtual environments and their purpose in Python projects.
Understand how to create and manage virtual environments.
Get hands-on experience with setting up and using a virtual environment.
Managing Python environments is essential for keeping project dependencies isolated and avoiding version conflicts. Python provides several tools for managing environments, including Conda and the built-in venv module. Both help to create isolated environments for Python projects, each with its own set of dependencies.
Conda is a package, dependency, and environment management tool that simplifies managing different Python versions and libraries. It is especially popular in the scientific community and on Windows platforms.
A Conda environment is a self-contained directory that contains a specific collection of Conda packages. These environments help isolate different projects from each other, ensuring that dependencies for one project do not interfere with others.
Avoid installing packages into the base Conda environment. Create a new environment for each project to maintain isolation.
To create a new Conda environment, use the following command:
admin@MacBook~ $ conda create -n <env_name> python=<version#>
For example, to create a Conda environment named ml-climate with Python 3.12:
admin@MacBook~ $ conda create --name ml-climate python=3.12
To activate the environment:
admin@MacBook~ $ conda activate ml-climate
Install packages within an environment using conda or pip:
admin@MacBook~ $ conda install <package_name>
Or, if the package is not available via Conda:
admin@MacBook~ $ pip install <package_name>
Export an environment configuration to a file for sharing with colleagues:
admin@MacBook~ $ conda env export --no-builds --file environment.yaml
This creates a YAML file that lists all packages and dependencies used in the environment. Others can use this file to recreate the same environment.
To deactivate a Conda environment:
admin@MacBook~ $ conda deactivate
To remove an environment:
admin@MacBook~ $ conda env remove --name <env_name>
venvThe venv module, included in Python by default, is another tool for creating isolated environments. While Conda is a more comprehensive solution, venv is lightweight and works well for basic Python projects.
venvTo create a virtual environment, follow these steps:
Ctrl + Alt + T or search for Terminal.Navigate to the Desired Directory:
Use cd to move to the project folder:
admin@MacBook~ $ cd Documents/ml-climate
Create the Virtual Environment: Use the following command:
admin@MacBook~ $ python3 -m venv ml-climate
admin@MacBook~ $ source ml-climate/bin/activate
Install Packages:
Install dependencies with pip:
admin@MacBook~ $ pip install <package_name>
Deactivate the Virtual Environment (Optional): When done, deactivate the environment:
admin@MacBook~ $ deactivate
venvWhile in the virtual environment, use pip to install, update, or uninstall packages:
Install a package:
admin@MacBook~ $ pip install <package_name>
To list installed packages:
admin@MacBook~ $ pip list
To uninstall a package:
admin@MacBook~ $ pip uninstall <package_name>
myproject and install requests.fetch_page.py using requests to fetch a webpage.Example script:
import requests
response = requests.get('https://www.example.com')
print(response.text)
Activate the Virtual Environment and install requests:
admin@MacBook~ $ source myproject/bin/activate
admin@MacBook~ $ pip install requests
Run the Script and deactivate the environment when done:
admin@MacBook~ $ python fetch_page.py
admin@MacBook~ $ deactivate
To set up Python environments using Conda on Windows via PowerShell, follow these steps. Ensure that Anaconda or Miniconda is installed before proceeding.
Verify Conda Installation: Open PowerShell and check if Conda is installed:
PS C:\> conda --version
If Conda is installed correctly, it should return the version number.
Once Conda is set up, you can create and manage environments as follows:
Create a New Environment:
To create a new environment with a specific Python version, use the conda create command. For example, to create an environment called ml-climate with Python 3.12:
PS C:\> conda create --name ml-climate python=3.12
Activate the Environment:
After creating the environment, activate it using the conda activate command:
PS C:\> conda activate ml-climate
Install Packages: Once the environment is activated, you can install packages like this:
PS C:\> conda install <package_name>
Deactivating the Environment: To exit the environment and return to the base environment, run:
PS C:\> conda deactivate
Remove an Environment: If you no longer need an environment, you can remove it with the following command:
PS C:\> conda env remove --name <env_name>
To export the configuration of a Conda environment to a .yaml file (useful for sharing), run:
PS C:\> conda env export --no-builds --file environment.yaml
This creates a YAML file that lists all installed packages, dependencies, and channels used for installation.
To recreate an environment from a .yaml file, use the following command:
PS C:\> conda env create --file environment.yaml
Here’s the revised exercise with the environment name changed to ml-flow.
PS C:\> conda --version
You should see the version of Conda installed.
ml-flow with Python 3.10:
PS C:\> conda create --name ml-flow python=3.10
ml-flow environment:
PS C:\> conda activate ml-flow
(ml-flow) before the command prompt, indicating that the environment is active.numpy and matplotlib:
PS C:\> conda install numpy matplotlib
numpy and matplotlib are installed by running:
PS C:\> conda list
numpy and matplotlib in the list of installed packages.PS C:\> conda deactivate
ml-flow environment configuration to a YAML file:
PS C:\> conda env export --no-builds --file ml-flow.yaml
ml-flow.yaml file has been created in your current directory.PS C:\> conda env remove --name ml-flow
conda env list.Key Points
Virtual environments isolate dependencies.
Using
venvfor creating environments.Activating and deactivating virtual environments.