Skip to content

Run and Test Your First Script

First PublishedByAtif Alam

This page is for beginners who already installed uv on a Mac (Environment setup). You will:

  1. Create a project folder.
  2. Write a few lines of Python and run them.
  3. 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).
  4. 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.


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”):

Terminal window
mkdir hello_uv
cd hello_uv
uv init --python 3.12
uv venv

What each command does:

CommandWhat it does
mkdir hello_uvCreates a new folder named hello_uv.
cd hello_uvMoves you inside that folder.
uv init --python 3.12Creates main.py and pyproject.toml (a small config file for the project).
uv venvCreates .venv/ — your project’s private Python environment.

You should now see something like:

hello_uv/
pyproject.toml
.venv/
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 called name.
  • message = "Hello, " + name + "!" — build a new string and store it in message.
  • print(message) — show message in the terminal.

Run it from inside the hello_uv folder:

Terminal window
uv run python main.py

You 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.

Terminal window
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:

Terminal window
source .venv/bin/activate
which python
python main.py
deactivate

After activate, your shell prompt may show (.venv). The which python path should end with hello_uv/.venv/bin/python.


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:

Terminal window
uv run python main.py

You 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.


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:

Terminal window
uv add --dev pytest

The --dev flag means “only needed for development/testing,” not for running the app in production.

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.”

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 in main.py (pytest loads main.py without running the print).
  • def test_message(): and def test_name():pytest looks for functions whose names start with test_. You are not writing app logic here; this is pytest’s required shape. You will learn def properly in Language basics.

Run tests:

Terminal window
uv run pytest
uv run pytest -v

Expected:

2 passed

You can delete the assert inside if __name__ == "__main__": in main.py now — the test file covers the same checks.


  • Language basics — variables, if, loops, and later def functions.
  • Process log files — a longer stdlib script; save it as another .py file in the same project and run with uv run python process_logs.py.
  • Install another packageuv add package-name (see Translating pip commands to uv if a tutorial says pip install).
  • 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 pytest then uv run pytest — finds test_*.py files and runs functions named test_....
TermPlain meaning
venv (.venv/)A private Python + packages folder for this project only.
uv runRun a command inside the project venv without activating it manually.
pytestA tool that finds and runs test files.
importLoad code or variables from another file in the same project.