How to create a Python virtual environment
A virtual environment works in only a specified folder, and doesn’t influence other environments. By using it, you can avoid version conflicts of Python packages between different projects.
Follow these steps to set up your environment quickly.
2 Create a virtual environment
Enter the following command to create a environment folder named .venv.
py -m venv .venv[!TIP] Specific Python Version
If you have multiple versions installed, specify one like this:py -3.xx -m venv .venv
3 Activate the environment
To start using the virtual environment, you need to activate it.
.venv\Scripts\activateVerify the activation: Check which Python version is currently active:
py -V4 Install packages
Once activated, you can manage your libraries without affecting the global system.
4.1 Individual Installation
pip install [Package Name]To install a specific version:
pip install [Package Name]==[Version No.]4.2 Batch Installation (using requirements.txt)
- Export existing list:
pip freeze > requirements.txt - Install from list:
pip install -r requirements.txt
[!NOTE] You can confirm the list of installed packages anytime using:
pip list
5 Deactivate the environment
When you are finished working, exit the virtual environment with:
deactivate6 Configure your IDE
To make the most of your virtual environment, set the Python interpreter in your IDE (e.g., VS Code or PyCharm).
Interpreter Path: .venv\Scripts\python.exe