Running Your Project With uv
00:00
So you’ve seen a number of files that uv
creates as soon as you initialize your projects. But you’ve noticed that there’s a file main.py
that gets created as well.
00:11
It’s a stub Python file and it contains a couple of lines of code that you could run already. But how do you run the code in your project? Well, there’s a uv
subcommand for that.
00:22
When you are managing a project with uv
, you will spend a lot of time using the uv
CLI. You use uv init
to initialize your project and you can use the subcommand uv run
to run your project.
00:37
So let’s go ahead, clear the screen. If you type uv run
and then the name of your Python file and if you press Enter, uv
runs your code for you.
00:49
And because this was the very first time that uv run
was used inside your project, not only do you get the output from the main.py
file, but you also get two lines of output that were created by uv
because uv
will try to manage a virtual environment for you.
01:09
You can see that uv
said creating virtual environment at venv
. And by using uv run
you ensure that whenever you run your project, you are running inside your virtual environment.
01:21
And this allows you to have your project isolated from everything else in your computer and from other projects. And the very first line of output was just uv
reminding you of the Python version that it’s using.
01:33
So if you list the contents of your project folder, you can see that there’s two new things here. There’s .venv
, the directory for your virtual environment.
01:43
This was created by uv
because this was the first time you ran code in your project. And there’s also a file uv.lock
. Now this uv.lock
file is a very interesting file.
01:54 Let’s clear the screen and inspect its contents. Right now, it’s a fairly short file and you can almost kind of read the file and understand what’s going on.
02:07
This uv.lock
file is a log file throughout the lifetime of a project, it’ll contain all the dependencies of your project. For now, you have none, only the Python language itself.
02:20 It’ll contain all dependencies of your project, even the things you depend on indirectly. And it’ll contain the exact versions you have installed on your system.
02:32 And the reason you want this is because then you push this file to your version control system so that other people have access to this file, allowing them to reproduce your virtual environment to a T.
02:48 And by having others reproduce your virtual environment exactly, you ensure that everyone is using and or developing your project under the exact same conditions.
03:01
This uv.lock
file is managed by uv
. You do not want to edit it by hand, but you want to push it to version control at all times.
03:12
So just a quick recap of everything our project contains. We’ve got version control files, the standard .git
directory, and the file .gitignore
, which is already pre-populated with some useful things to ignore.
03:26
You have the .python-version
file that tells uv
what’s the default Python version for this project. You have the main.py
, which is just a stub Python file.
03:36
You might want to put your code there or replace it with something else entirely. You have the pyproject.toml
file, which contains project metadata that you can edit by hand and that we will edit by hand in future lessons.
03:49
There’s the README
file where you’ll have basic project documentation, basic instructions, basic setup instructions. There’s also uv.lock
, which is your CREs platform log file, where uv
keeps track of dependencies and important information.
04:03
You’ll know you don’t want to edit this file by hand, but you will want to push it to version control because it’s a very important file. And finally, there’s your .venv/
folder, which you might have seen in other tools.
04:16
You might have used these already. It’s a folder that contains your virtual environment where your dependencies will end up and it’s automatically managed by uv
.
04:25
And in the next lesson, you will see how to use uv
to manage it.
04:30 So now that you know how to create a project and now that you’re comfortable with the structure of the project and you understand more or less what every file does, in the next lesson we’ll learn how to manage dependencies within your project.
Become a Member to join the conversation.