Virtual Env
A virtual environment is an isolated Python system.
python -m pip install --upgrade pip
pip install virtualenv
virtualenv ./my_env # create
. my_env/bin/activate # load and activate
pip list # test env
deactivate # exit env
source my_env/bin/activate # second method
pip list
deactivate
: '
Package Version
---------- -------
pip 22.3.1
setuptools 65.6.3
wheel 0.38.4
'
echo 'my_env/' > .gitignore
Venv
Use the venv module which is included in the standard library.
python3 -m venv myenv
myenv\Scripts\activate # windows
source myenv/bin/activate # linux
Requirements
Save the list for requirements in order to used later (or on another computer).
(.venv) pip freeze > requirements.txt
(.venv) pip install -r requirements.txt
Python Upgrade (Windows)
1. Download Python
Go to the official Python website: https://www.python.org/downloads/windows/ Download the “Windows x86-64 executable installer” (64-bit is standard for servers). You just need to use the right installer and options. The installer contains everything Python needs. Download exactly this file:
Windows x86-64 executable installer
python-3.12-amd64.exe
2. Install
Run the installer -> Run as Administrator Add Python to PATH. Unblock the File: Windows marks files copied from another machine as 'from the internet' even if transferred by USB. Solution: - Right‑click python-3.13.0-amd64.exe - Properties - Check Unblock - Click OK - Run installer again Python on Windows is designed to support multiple versions installed at the same time. In Path, move the new version above the old one.
python --version
3. Virtual Environments
Each project uses its own Python version.
C:\Python312\python.exe -m venv venv312
venv312\Scripts\activate
Upgrade pattern (production-safe):
- Install new Python side‑by‑side (PATH)
- Create new venv with it
- Reinstall dependencies
- Switch service / app to new venv
- Remove old venv when stable
3.1 Create a new virtual environment
From the application root directory:
"D:\Program Files\Python312\python.exe" -m venv venv312
venv312\Scripts\python.exe --version
# Python 3.12.0
3.2 Reinstall dependencies
cd D:\python-apps\pof_statistics\
.\venv312\Scripts\activate
(venv312)
python.exe -m pip install --upgrade pip
pip install -r requirements.txt
Questions and answers:
Clink on Option to Answer
1. What is a virtual environment in Python?
- a) An isolated Python environment with its own packages
- b) A backup copy of the operating system
2. Why are virtual environments useful?
- a) They isolate project dependencies from the system Python
- b) They make Python programs run faster
3. What is the main difference between virtualenv and venv?
- a) venv is included in the Python standard library
- b) virtualenv only works on Windows
4. What happens when you activate a virtual environment?
- a) Python uses the environment’s local packages and interpreter
- b) Python deletes system-wide packages
5. Why should a virtual environment folder be added to .gitignore?
- a) Because it can be recreated and should not be version-controlled
- b) Because Git cannot track folders
6. What is the purpose of deactivating a virtual environment?
- a) To return to the system’s global Python environment
- b) To uninstall Python
7. Which tool is recommended when you want only standard-library tools?
- a) venv
- b) virtualenv