Run and Test Your First Script
This page is for beginners who already installed uv on a Mac (Environment setup). You will:
- Create a project folder.
- Write a few lines of Python and run them.
- Optionally check that Python is using your project’s isolated environment (a virtual environment, or venv — a private copy of Python and packages for this folder only).
- Try a simple automated test with pytest (a popular test tool).
Run each command in Terminal right after the step that shows it. You should see output similar to what each step describes.
What you will have at the end: a folder called hello_uv/ containing main.py, test_main.py, and a hidden-style .venv/ folder that holds your project’s Python.
You do not need to write any def functions in this walkthrough. Your script is just variables and a print statement.
Step 1 — Create the Project Folder
Section titled “Step 1 — Create the Project Folder”Goal: Make a new folder and tell uv to set up Python for it.
Run these commands one at a time (or copy the block — && means “run the next command only if the previous one succeeded”):
mkdir hello_uvcd hello_uvuv init --python 3.12uv venvWhat each command does:
| Command | What it does |
|---|---|
mkdir hello_uv | Creates a new folder named hello_uv. |
cd hello_uv | Moves you inside that folder. |
uv init --python 3.12 | Creates main.py and pyproject.toml (a small config file for the project). |
uv venv | Creates .venv/ — your project’s private Python environment. |
You should now see something like:
hello_uv/ pyproject.toml .venv/ main.pyStep 2 — Write and Run main.py
Section titled “Step 2 — Write and Run main.py”Goal: Put three lines of Python in main.py and run the file.
Open main.py in any text editor. Replace everything in the file with:
name = "world"message = "Hello, " + name + "!"print(message)Line by line:
name = "world"— store the text"world"in a variable calledname.message = "Hello, " + name + "!"— build a new string and store it inmessage.print(message)— showmessagein the terminal.
Run it from inside the hello_uv folder:
uv run python main.pyYou should see:
Hello, world!uv run runs Python using your project’s .venv — you do not need to run source .venv/bin/activate first.
If you get No such file or directory, make sure you ran cd hello_uv and that main.py is saved in that folder.
Step 3 — Optional: Check Which Python You Are Using
Section titled “Step 3 — Optional: Check Which Python You Are Using”Goal: Confirm Python is coming from .venv, not macOS system Python.
This step is optional. Skip it if the script already printed Hello, world! and you want to keep moving.
uv run python -c "import sys; print(sys.executable)"You should see a path that includes hello_uv and .venv. That means your project environment is in use.
Another way some tutorials use — turn the environment on manually, then turn it off:
source .venv/bin/activatewhich pythonpython main.pydeactivateAfter activate, your shell prompt may show (.venv). The which python path should end with hello_uv/.venv/bin/python.
Step 4 — A Quick Self-Check With assert
Section titled “Step 4 — A Quick Self-Check With assert”Goal: Ask Python to verify your message is correct before printing it.
An assert line means: “stop with an error if this is not true.” Useful for a tiny script; later you will use a test file instead.
Update main.py to:
name = "world"message = "Hello, " + name + "!"
assert message == "Hello, world!"print(message)Run it again:
uv run python main.pyYou should still see Hello, world!. If you change name to something that breaks the expected message, Python stops with AssertionError — that is the check working.
Step 5 — Add pytest and a Test File
Section titled “Step 5 — Add pytest and a Test File”Goal: Run checks in a separate file so your main script stays simple.
pytest runs test functions in files whose names start with test_. Online tutorials often say pip install pytest; with uv you run:
uv add --dev pytestThe --dev flag means “only needed for development/testing,” not for running the app in production.
Move print so tests do not run it twice
Section titled “Move print so tests do not run it twice”When pytest loads main.py, Python executes the whole file. If print(message) sits at the top level, tests would print every time too.
Wrap the run logic like this (add the last two lines — not a function, just a guard Python beginners see often):
name = "world"message = "Hello, " + name + "!"
if __name__ == "__main__": assert message == "Hello, world!" print(message)Plain English: if __name__ == "__main__": means “only run the indented lines when I execute this file directly (uv run python main.py), not when something else imports this file.”
Create test_main.py
Section titled “Create test_main.py”In the same folder as main.py, create a new file test_main.py:
from main import message, name
def test_message(): assert message == "Hello, world!"
def test_name(): assert name == "world"What this means:
from main import message, name— read the variables you set inmain.py(pytest loadsmain.pywithout running theprint).def test_message():anddef test_name():— pytest looks for functions whose names start withtest_. You are not writing app logic here; this is pytest’s required shape. You will learndefproperly in Language basics.
Run tests:
uv run pytestuv run pytest -vExpected:
2 passedYou can delete the assert inside if __name__ == "__main__": in main.py now — the test file covers the same checks.
Step 6 — What to Do Next
Section titled “Step 6 — What to Do Next”- Language basics — variables,
if, loops, and laterdeffunctions. - Process log files — a longer stdlib script; save it as another
.pyfile in the same project and run withuv run python process_logs.py. - Install another package —
uv add package-name(see Translating pip commands to uv if a tutorial sayspip install).
Key Takeaways
Section titled “Key Takeaways”mkdir+uv init+uv venv— create a project with its own Python in.venv/.uv run python main.py— run your script using that environment.assert— a one-line “this must be true” check; fine for learning, pytest scales better later.if __name__ == "__main__":— run code only when the file is executed directly, not when imported by tests.uv add --dev pytestthenuv run pytest— findstest_*.pyfiles and runs functions namedtest_....
Words You Might See
Section titled “Words You Might See”| Term | Plain meaning |
|---|---|
venv (.venv/) | A private Python + packages folder for this project only. |
| uv run | Run a command inside the project venv without activating it manually. |
| pytest | A tool that finds and runs test files. |
| import | Load code or variables from another file in the same project. |