If you’re working on your application, you’d typically work in your virtual environment and use a package manager like pip, conda, or the most recently-famous uv.
Say that you wanted to test a new idea or create a one-off script, but it required either a new dependency or a whole new virtual environment. You can do 2 things:
- Deactivate your virtual environment and pollute your global environment with the dependencies (not ideal) 
- Add in your dependency into an environment, if it exists. 
Worst case scenario, you could just create a new virtual environment, activate it, and do your work there.
But there’s an easier alternative using uv, and it’s as simple as inserting some code at the top of your Python file.
Declaring Dependencies in your Script
To declare your dependencies in your script, add the following lines to the top of your Python file:
# /// script
# dependencies = []
# ///Note: you may have #!/usr/bin/env python instead. We’ll handle this a little later.
Now, you can declare your dependencies in the dependencies list:
# /// script
# dependencies = ["pandas", "numpy", "matplotlib"]
# ///And then you’ll run your script using uv run :
$ uv run script.pyThis will create a temporary virtual environment right before the script executes, install all the dependencies that are listed under the dependencies list, executes the script, and then deletes the temporary virtual environment shortly afterward.
This is a game changer for those who use Python for one-off scripts because it removes the requirement of knowing how to create the environment, remembering to activate it, and then remembering to delete it when you’re finished 🤯.
As someone who has an endless sea of conda and venv-based environments, I am so relieved that I no longer need to manage some of these environments.
Working With an Example
Let’s work with this simple example where we’re applying a 10% sales tax:
import pandas as pd
df = pd.read_csv("sales.csv")
# Filter out rows with zero sales
df = df[df["amount"] > 0]
# Add a new column with 10% sales tax applied
df["total_with_tax"] = df["amount"] * 1.10
df.to_csv("sales_with_tax.csv", index=False)Normally, you’d have to create (or find and use) a virtual environment that has pandas pre-installed. But suppose that we don’t have an environment with pandas installed. We can add in our dependencies straight into our script:
# /// script
# dependencies = ["pandas"]
# ///
import pandas as pd
df = pd.read_csv("sales.csv")
# Filter out rows with zero sales
df = df[df["amount"] > 0]
# Add a new column with 10% sales tax applied
df["total_with_tax"] = df["amount"] * 1.10
df.to_csv("sales_with_tax.csv", index=False)Then run it using uv: 
$ uv run sales_tax.pyThe biggest takeaway here is that we don’t need to create a new or search for an environment to execute this one-off script. It’s created when you run the uv run command and gets deleted afterwards.
Keeping the script executable
Sometimes, you may have a shebang line in your script at the very top, which allows you to run python my_script.py in the command line.
To keep the script executable, you’ll want to use this at the top of your file instead:
#!/usr/bin/env -S uv run --script
# /// script
# dependencies = ["pandas"]
# ///Happy coding!
📧 Join the Python Snacks Newsletter! 🐍
Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:
- Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above. 
- Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email. 
- Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away. 
You can unsubscribe at any time.
Interested in starting a newsletter or a blog?
Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.
If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.
This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.


