avatarKeisuke Daimon

Summary

This article provides a step-by-step guide on how to use the venv module to create and manage virtual environments in Python 3.12.

Abstract

The article "How to Use venv in Python 3.12" is a comprehensive tutorial that explains the process of setting up and using virtual environments with the venv module in Python versions 3.7 and above. It begins by instructing the reader to navigate to the desired directory for the virtual environment setup, followed by creating the environment using the python -m venv .venv command. The article then details the activation process for both Mac OS and Windows PowerShell, noting the command differences between the two systems. It also covers how to install libraries using pip, manage package dependencies with requirements.txt, and deactivate the virtual environment. Additionally, the article provides guidance on integrating the virtual environment with VSCode and Jupyter Notebook, including selecting the correct Python environment for a Jupyter Notebook.

Opinions

  • The author prefers naming the virtual environment directory .venv for convenience.
  • The use of pip freeze > requirements.txt is recommended for capturing the current environment's package list, which facilitates reproducibility.
  • The article implies that using virtual environments is a best practice for Python development to avoid package version conflicts and maintain project-specific dependencies.
  • Visual examples are provided for VSCode and Jupyter Notebook integration, suggesting that these tools are commonly used in conjunction with virtual environments.
  • The reference to the official Python documentation for venv indicates the author's reliance on authoritative sources for accurate and up-to-date information.

How to Use `venv` in Python 3.12

This story simply explains how to use venv in Python 3.12 (precisely speaking, Python 3.7 or later).

Environment

  • Python 3.12.1

Actions

0: Go to the directory where you want to set up venv

cd <path/to/dir>

1: Create a new environment

.venv is a directory to store everything about the virtual environment. The name can be anything, but I personally prefer using .venv.

python -v venv .venv

2: Activate the environment

Mac OS and Windows have different commands.

# Mac OS
source .venv/bin/activate

# Windows PowerShell
.venv\Scripts\Activate.ps1

If the environment is successfully activated, every line of the Terminal shows (.venv).

3: Use pip to install libraries

# Newly install libraries
pip install <library name>

# Install libraries using requirements.txt
pip install -r requirements.txt

# Create requirements.txt
pip freeze > requirements.txt

4: Deactivate the environment

deactivate

Actions for VSCode & Jupyter Notebook

1: Open a New Jupyter Notebook

Open a new .ipynb file and cliek “Select Kernel” on the right.

2: Click “Python Environments…”

3: Choose a Python Environment

The path to the venv environment should be shown like .venv/bin/python.

References

Python
Venv
Recommended from ReadMedium