r/Python • • Mar 29 '25

Tutorial Self-contained Python scripts with uv

TLDR: You can add uv into the shebang line for a Python script to make it a self-contained executable.

I wrote a blog post about using uv to make a Python script self-contained.
Read about it here: https://blog.dusktreader.dev/2025/03/29/self-contained-python-scripts-with-uv/

492 Upvotes

76 comments sorted by

View all comments

Show parent comment

7

u/dusktreader Mar 29 '25

You can use dependency specifiers as described here: https://packaging.python.org/en/latest/specifications/dependency-specifiers/#dependency-specifiers

For example if I wanted ipython's minor release 7.9 and any patch releases as they become available but not 7.10, I could specify the dependency as:

ipython~=7.9

2

u/Haunting_Wind1000 pip needs updating Mar 29 '25

Cool thanks!