7 stages2 sections22 units

01.01 Lesson 1 of 10

36 min

Set 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 .py file 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

Exercise

Create your first project folder

Create a clean workspace for the Bike Sharing Dataset Explorer. Do not install packages globally.

  1. Create a folder named bike-sharing-explorer.
  2. Create and activate .venv inside it.
  3. Create main.py and print a short project title.
  4. Install pandas, numpy and matplotlib inside the environment.
  5. Save the installed packages in requirements.txt.
Done when

A fresh terminal can activate the environment, run main.py and install the recorded dependencies.

Free references

Study resources

2 links
  1. 01
    Creating virtual environments Article

    Read the creation and activation examples; note the Windows and POSIX activation paths.

  2. 02
    Using the Python interpreter Article

    Read the first sections on invoking the interpreter and running scripts.

Check your understanding

Self-test

2 questions
  1. Question 1

    Why should a project use its own virtual environment?

  2. Question 2

    What does requirements.txt record?

Completion check

Move on when

You can create the environment, activate it, install dependencies and run the project from a fresh terminal.

Quick find

Search Michi

Type to search every stage, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels