Python Virtual Environments
Each project keeps its own packages in a separate folder. The usual name of that folder is .venv. You create it from the terminal, with python -m venv .venv. You do not save that line inside the program file. The folder belongs to one project. Another project gets another folder. math still comes with the language. This command does not install requests, and it does not replace math.
python -m venv .venv
The terminal is where this line runs. Its job is to create the folder .venv. You do not get a screen of 2 or of 3, and you do not see the word requests. The same words inside a program file stop before the folder exists. The name of that stop is SyntaxError. None is not written in its place. The file does not continue, and .venv is not created. venv is a word the terminal understands after python -m. It is not a name you defined in the program.
from pathlib import Path
print(Path(".venv").name)
The screen shows .venv. That is the folder's name, and nothing more. Path builds the name. print writes it. The folder is not created by this program. If .venv was missing before these two lines, it is still missing after them. Creation is the terminal command above. This program only shows you the spelling of the name. The dot at the front is part of the name. It is not a decimal point, and it is not subtraction.
The previous page typed pip install requests in the terminal. That install writes where the terminal's python is aimed. After you switch the terminal into .venv, the install writes inside that folder. Before the switch, it writes in the shared place, and this project does not own it. The spelling of the switch is not the same on every system, so that line is not on this page. Your program file stays outside .venv. The folder holds the environment. It does not hold the lesson you are writing. math is still there, because it comes with the language, including inside this folder.
The next page is classes. This page only makes one project folder, shows that its name is .venv, and keeps the program file outside it.
One folder per project
python -m venv .venv creates a folder named .venv. The same words in a program file stop as SyntaxError. print of the name shows .venv and does not create the folder. After you switch, pip writes inside that folder. The program file stays outside. The next page is classes.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top