01.01 Lesson 1 of 10
36 minSet Up a Python Project
Run Python from the terminal, create an isolated environment and keep a small project reproducible.
By the end of this lesson
You will be able to
- Run a
.pyfile from the terminal. - Create and activate a project-specific virtual environment.
- Install a package and record it in a requirements file.
The interpreter runs your code
Python can execute a file or accept statements interactively. Use the interactive prompt to test a small expression; use a .py file when you want a repeatable program that can be run again.
python --version
python
>>> 2 + 3
5
python main.py Keep project dependencies isolated
A virtual environment gives one project its own package directory. This avoids accidental dependency conflicts between unrelated projects. Create it in the project folder and activate it before installing packages.
python -m venv .venv
# Windows PowerShell
.venv\Scripts\Activate.ps1
# macOS / Linux
source .venv/bin/activate
python -m pip install numpy pandas matplotlib
python -m pip freeze > requirements.txt Apply what you learned
ExerciseCreate your first project folder
Create a clean workspace for the Bike Sharing Dataset Explorer. Do not install packages globally.
- Create a folder named
bike-sharing-explorer. - Create and activate
.venvinside it. - Create
main.pyand print a short project title. - Install
pandas,numpyandmatplotlibinside the environment. - Save the installed packages in
requirements.txt.
A fresh terminal can activate the environment, run main.py and install the recorded dependencies.
Free references
Study resources
- 01 Creating virtual environments Article
Read the creation and activation examples; note the Windows and POSIX activation paths.
- 02 Using the Python interpreter Article
Read the first sections on invoking the interpreter and running scripts.
Check your understanding
Self-test
-
Question 1
Why should a project use its own virtual environment?
It isolates that project’s installed packages from other projects and the base Python installation. -
Question 2
What does
requirements.txtrecord?A list of package requirements that can be installed later. It does not contain the virtual environment itself.
Completion check
Move on when
You can create the environment, activate it, install dependencies and run the project from a fresh terminal.