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.ps1If 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.txt4: 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.





