First project
Create your first project
We now create the project you will use during the whole introduction. Every lesson ends with something you can add to it.
- Create the project with uv
- Run it
- Open it in Zed
- Add pytest and write your first test
- Check your code with ruff and ty
- Bonus: put it on GitHub
First project · 1
Create the project
Open a terminal and go to the folder where you keep your stuff, for example Documents:
cd ~/DocumentsThen let uv create the project and step into it:
uv init --no-package learn-python
cd learn-python--no-package keeps things simple: your code lives right in the folder, in main.py. Without it, uv puts the code in a src/ folder instead, which
you will meet in the last lesson.
uv made a folder with these files:
learn-python/
├── .git/ ← history of your changes (hidden folder)
├── .gitignore ← files git should ignore
├── .python-version ← which Python version this project uses
├── README.md ← a description of the project
├── main.py ← your program starts here
└── pyproject.toml ← the project's name, settings and packagesFirst project · 2
Run it
uv run main.pyThe first time, uv downloads Python and sets everything up. That can take a minute. Then you should see:
Hello from learn-python!uv run is how we always start programs in this course. It makes sure the right Python
and the right packages are used.
First project · 3
Open it in Zed
In the terminal, inside the project folder:
zed .Open main.py in the file list on the left. Replace everything in it with this:
def greet(name: str) -> str:
return f"Hello, {name}!"
def main() -> None:
print(greet("pirate"))
if __name__ == "__main__":
main()Save with Ctrl + S (Mac: ⌘ + S) and run it again
with uv run main.py.
First project · 4
Add pytest and write a test
A test is a small program that checks that your code works. Add pytest to the project:
uv add --dev pytestMake a folder called tests with a file test_main.py inside:
from main import greet
def test_greet():
assert greet("Ada") == "Hello, Ada!"Add these two lines at the bottom of pyproject.toml, so pytest knows where your
code lives:
[tool.pytest.ini_options]
pythonpath = ["."]Now run the tests:
uv run pytestYou should see 1 passed in green. Try changing "Hello, Ada!" in
the test and run it again. What happens?
First project · 5
Check your code with ruff and ty
ruff looks for mistakes and untidy code. ty checks that the types make sense.
ruff check .
ruff format .
ty checkTry to break something on purpose. In main.py, change the last line of greet to:
return f"Hello, {name}!" + 1Zed should underline it in red before you even run it. That is ty telling you that you cannot add a number to text. Change it back afterwards.
First project · 6
Bonus: put it on GitHub
uv already prepared git for you. Save a snapshot of your project:
git add .
git commit -m "My first project"You logged in with the GitHub CLI during setup, so one command creates the repository on GitHub and uploads your code:
gh repo create learn-python --source=. --public --pushThen open it in the browser:
gh repo view --webYour code is online! 🎉 Next time you change something, it is just git add ., git commit -m "what I did" and git push.
If gh says you are not logged in, run gh auth login again like in the
setup.