if you're still using pip or manually creating your own virtual environments then you need to watch this video in just a few minutes I'm going to completely change your life if you write any code in Python and that's because I'm going to show you this fantastic tool called UV now UV is a Python package and project manager that is almost 100 times faster than pip in certain situations yes you heard me correctly you can kind of see the example here from the website and what this does is take care of all of the Python tools like virtual environments installing dependencies using pip with just one single tool you're going to see how it works in this video but I promise you it is significantly better than creating your own virtual environments making the requirements.txt files installing them with pip and after you learn about this it's the only thing that you're going to use from now on so let's go ahead and get into the video so first things first we do need to install this tool now I'll leave this link in the description so you can get directly to it now if you're on Mac or Linux you can use something like curl or wget to install the tool or you can actually just directly use pip or pi so you can actually just go and say pip install uv and then you can use it directly from your terminal now if you're on windows same thing you can use powershell here or again the easiest way is just directly to use pip so pip install uv now if I just open up my terminal here so let's get one open i'll just show you the command what I would personally run so if I bring this on screen literally all you need to do the easiest way to do it if pip works on your system is just type pip install uv since I'm on Mac here I'm going to change this to pip 3 install uv and then you can see it's already installed here because well I installed it previously anyways once you've got it installed then you can restart your terminal and you can start using it let me show you how it works okay so what I've done here is I've just opened up VS Code and I've opened up a new folder called UV demo now you can do this anywhere you want but I'm just using VS Code for the terminal and so we can see all of the files in my editor now what I'm going to do to start is I'll just make a file here called main.py and I'm just going to print out the current Python version so I'm going to say import sys and I'm going to print and then this is going to be syss dot and then version okay so now from my terminal if I want to start using UV all I have to do is type the UV command when I do that you'll see that it kind of shows this here and if you get some output and it shows you the various commands that you can run then you know that this is working now first things first the documentation for this is really good so you can just read the docs directly from the web but obviously this video will clarify a few things and make it a lot easier to get started so now that UV is installed the first thing that's interesting here is that we can install various Python versions so for example I can type uv Python and then I can type list and this will list all of the available Python versions that I could install and show me the ones that I have downloaded so you can see here that I already downloaded a few just when I was playing with this tool before the video and it shows us other versions that we can download now this only works with Python version 3.7 and above and actually I think above 3.7 so you can see it's showing us all of the 3.8 versions here so if you want something prior to that then you're not going to be able to use UV anyways if we want to install a particular Python installation we can type uv Python install and then we can just specify the version so if I want something like 3.8 8 i can do uv python install 3.8 and then it's going to go and grab that for me and install it and notice this runs incredibly fast even though I do have quick internet here and is significantly faster than pip because it's written in rust and it has a lot of optimizations behind the scene so now that we have that installed again we can list out the versions we also can search for particular versions if we do something like uv python find and then something like 3.8 and it will show us where that version is and which one we have installed if you want to uninstall a version you can type uv Python and then uninstall like that and then specify the version that you want to uninstall okay so that's getting the different Python versions installed but now I want to show you how we use them to run scripts and then set up Python projects now before we get into that I want to take a second to tell you about another gamechanging tool related to dependencies and that's Mend Renovate the sponsor of today's video if you're like me and you're constantly battling outdated dependencies then Mend Renovate is a lifesaver it automatically scans your repositories for outdated open-source packages and even your private modules it then creates pull requests with the latest updates complete with release notes and insights so there's no more need for manual checking now what really sets this apart is the merge confidence ratings by analyzing factors like release age adoption rates and crowdsource test data it gives you a clear picture whether an update will integrate smoothly saving you from unexpected breaks which personally I have experienced far too many of in my dev career and by keeping all of your dependencies current Mend Renovate helps you reduce technical debt and focus on what matters which is writing great code and accelerating your release cycles so if you're looking to boost code quality reduce risk and keep your projects running smoothly then check out Mend Renovate it's the industry standard for automated dependency updates and it might just be what your workflow needs to keep it running smoothly anyways check it out i've left a link to it in the description and now let's get back to the video okay cool so now if we want to start running some Python scripts it's as simple as typing the command uv run and then the name of the script that we want to run in this case something like main.py now notice that it's just printing out the version that it's using and that's because that's what I printed in this script so that will just use the default version of Python which I believe is 3.11 however if we want to manually specify the version of Python to use when we're running a particular script then we can do the following we can say uv run and then dash- python and then after this we can specify the exact version so I can do something like 3.9.21 and then main.py and now notice that we're using Python version 3.9.21 i don't need to change anything i don't need to set up a virtual environment it's very simple you just specify directly in the UV command now same thing if we want to go back to maybe 3.10 or something again we can specify that and if we try to use a version that we don't have installed then it will just simply tell us that we don't have it installed and we can install it okay so that's how you run it with the Python version however there might be times when you want to run this code using a module right so something like the rich module which is not installed by default in Python in versions like 3.9 3.8 etc so again we're importing rich this is just kind of a terminal formatter in Python and in earlier versions of Python you would need to install this using something like pip so I'll show you what happens if I did do something like let's go back here uv run Python 3.9 main py you notice that we get an error and it says no module named rich now rather than having to set up an environment install all of these dependencies with uv we can actually specify that we want to include this in the run so I can do d-with and then rich let me just make this a bit smaller so you guys can read it and what this now says is that we're going to run this Python script with this dependency so it will automatically be installed and used for this run so if I hit enter here you can see now that this works and we don't get any errors and if I want to prove it to you further I can say from rich let's type this correctly import the print statement okay and now if we go here and we run this you can see that we get the formatting showing up okay so that's how you include a dependency let's say we wanted to install another dependency so maybe something like request then we could just use with multiple times so we say with rich and then d-with requests okay and you can see it installs all of those packages and then it runs the code and allows us to use it so that's the first advantage if you have Python scripts and they have a few different dependencies you can very easily run them without having to set up an environment and just have them be installed by default and then of course they'll be cached for future runs now let me show you a few other things you can do here when it comes to running Python scripts so obviously it can be a little bit annoying to have to write something like this out every time you run your Python script so UV actually has a solution for this and we can add the dependencies and Python version directly into the script so we just need to run the UV run command and then it will automatically know the configuration options to go with so let's go back here and let's type the following command to start i'm going to type uvinit-script then I'm going to do the name of my script so main.py py and then I'm going to specify the Python version that I want to use so Python and then 3.9.21 okay when I do this it's going to add this string at the top of my script or this comment sorry and here you can see that it specifies that we're requiring Python version greater than 3.9 now if I want to pin this exactly to 3.9 I can just put a double equal sign and now as long as we put the exact version so 3.9.21 we will use this when we run the script so now if I type uv run and then I run main.p py uh we'll see we're getting the error with rich and request because well I haven't actually installed these yet so let me just fix this and run and you can see we're using Python version 3.9.21 now the error that we just ran into actually tells me that I need to install some dependencies here as well so what I can do is add dependencies directly to this script now I can do that by just typing them in this dependency array or I can use the uv command to manage this for me so I can type uv and then add d-script again we can spell script correctly use our main.py and then we can just put the name of the dependency that we want so in this case rich when I do this you'll see that rich now gets added to the dependency array and if we wanted to continue here and add something like request we can do that as well and it gets automatically added sweet now if I come here and I uncomment this now watch what happens when I run my code you'll see that it installs these packages and then it runs the code using those dependencies okay so that was the first half of the video where we worked with individual Python scripts but now I want to show you how to work with Python projects now let's say you're creating a new Python project rather than making your own virtual environment or writing a requirements.txt txt file what you would do instead with UV is you would go into the directory where you want your code to exist so in this case UV demo2 and you would hit UVIT or type UVIT when you do that and you hit enter it's going to create a few different files for you here as well as initialize a git repository and you'll see that you get a git ignore file you get a Python version file which specifies the version that you're going to be using you get a main.py file kind of the entry point to your Python program you can of course change this you get a pi project.toml file and this is kind of equivalent to a requirements.txt file but it's a lot more detailed so you get things like the name of your project specifying the readme what Python version you actually need so the minimum version in this case you could also pin it to an exact version you get a dependency array and then inside of here this is where you would store all of the dependencies then of course you have your readme.markdown file and that's pretty much it now this is the main file that you want to pay attention to and of course you can modify this manually but you also can modify using UV so the advantage here is that UV is going to handle everything for you it also is automatically going to create a virtual environment for you and sync the virtual environment with the dependencies that you have and make it easier to work with other developers so just bear with me for a second so you can kind of see how this works so that's the UVNIT command and one thing I will mention you can use this command with an argument and if you give it an argument like my directory it will actually put all of these files in a new directory so I don't want that i want it in my existing directory so that's why I did this okay so let's say that we've now created this project obviously one of the first things we want to do is probably install a dependency so we can type uv add and then we can add something like the request module by hitting this okay now notice when we did that a few things happened first of all it resolved the packages installed the dependencies and did this insanely fast like way faster than it would happen if you're using pip it also went and modified the pi project.l file and added this request package now it added a version which is just the most up-to-date version for this python version but we could manually specify the version as well if we wanted to by going here and changing it right and kind of you know just specifying which one we want now we also created a few other files we created thisvnv directory now thisvnv directory is a virtual environment and this virtual environment is not something that you need to manually play with like you might need to do if you created it on your own it's handled by UV and it will automatically be synced whenever you change something in your project so for example if I go here and I were to manually add a package say like TensorFlow or something okay next time that I run my code it will automatically look at this and then install the various dependencies or remove the dependencies from my virtual environment to keep it up to date with my project i'm going to show that to you in just one second because TensorFlow is actually an example of one that won't quite work just for a reason you'll see in one second okay so anyways that's how you add a dependency again handled by UV and then you get this uv.lock file now this lock file is similar to something like a package json.lock file if you've ever worked with Node.js or React or npm and this is something that you would actually include in your source control so in a git repo because it specifies the exact versions of packages that are currently being used at this point so that developers can kind of look at this UV lock file or they can install based on it and reproduce behavior and make sure they have the exact same setup that you have on your computer so kind of a useful thing to have and again you would want to include this in your source control and it will automatically be synced with the project using UV okay so I'm going to show you now if you wanted to remove a dependency you could type uv remove and then again we could type something like request if we do that it's going to automatically uninstall the packages and then remove that from our pi project file now I want to show you something that could occur so let's say I try to do something like uv and then tensorflow now if I do this notice it actually gives me an error and it says all of the versions of TensorFlow have no wheels with a matching Python version and your project depends on TensorFlow so it's pretty much telling me like hey TensorFlow isn't available for Python version 3.13 so you have to use a different version so what I can do is I can downgrade my Python version by going here and changing this requires Python to maybe something like 3.10 and changing my Python version to something like 3.10 now that I've done that I can go here and I can add it and it just automatically updates my Python version for me syncs it with the project and then installs TensorFlow practically instantly now the reason it's going so fast is because I have this dependency cached but still even if you were to compare it to using pip it's just significantly faster so now TensorFlow is added all is good and we can start running our Python code so I can go UV run and then again my file main.py py and it runs and it automatically uses this virtual environment and all of the dependencies that we have installed in our project now just as another quick example let's say I were to go to main.py here and I were to import a package like pygame something that I don't have installed okay now if I were to import this of course now when I run my code so uvun and then main.py it's not going to work because pygame isn't installed so let's say I were to try to manually go here and add this as a dependency in my dependencies list now watch what happens when I run this notice that it actually automatically installs the package directly here from our dependency it'll then take a second because it's initializing pygame for the first time and then run and give us that output now the reason why this worked even though I didn't use the UV command to add this to my dependencies is because in the background what UV will do when you use this UV run command is it will run the UV sync command now the UV sync command is automatically going to sync your virtual environment with your dependencies in your toml so I can show you this if I were to delete these here from dependencies and then I run uvs sync you'll see that what it does is it removes all of these automatically for me okay so you can automatically or you can manually sorry run this uv sync command or you can just have it automatically run whenever you're running your Python script so you can manually adjust the dependencies array and then use the UV sync command or you can type uv and uv remove to remove and add these from your script now the last thing I'm going to show you is that if you want you can generate the lock file again by yourself manually by typing UV lock and this will then resolve everything and make a new lock file for you but if you run the UV sync command it will automatically generate the lock file as well obviously there's a lot more stuff you can do here with UV but that covers probably 95% of your use cases in a short video you can see that first of all it is blazingly fast it's also just a lot easier to use than managing your own virtual environments so please use UV it will automatically sync the virtual environment for you you can manage the dependencies very easily you can use this pi project.toml file oh and last thing if you want you can still go back to legacy pip by typing uv pip so if you type uvp pip you can still use the pip command as you normally would with UV i don't know why you would do that but they do have that option anyways guys I'm going to wrap up the video here if you enjoyed make sure to leave a like subscribe to the channel and I will see you in the next one [Music]