Mastering Python Projects with Ease: From Installation to Execution with uv!
Python is a magical programming language with countless amazing open-source projects, such as AI tools and video processing scripts. However, for many non-programmers, the biggest obstacle to downloading and running these projects from GitHub is "dependency management" and "version management."
Terms like pip, pyenv, venv, poetry, conda... just hearing their names can be dizzying, let alone struggling to get things running after a lot of effort. The good news is that there's a new tool called uv, which is simple, fast, and reliable, especially suitable for novice users. Today, I'll guide you step by step through uv, from installation to running a project, ensuring you can get started after reading this!
I. Installing uv on Windows: Done in Two Minutes
First, we need to install uv on your Windows computer. Don't worry, no complicated operations are needed, just follow me.
- Download and Install:
- Start Menu - find Windows PowerShell (right-click and select "Run as administrator" for more stability).
- Copy and paste this command in, then press Enter:bash
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
- This command will download the installation script from the uv official website and run it automatically. The process takes about a few seconds to a minute or two, depending on your network speed. After completion, the command line will prompt something like "uv installed successfully".
- Verify: Enter
uv --version
. If it displays a version number like "uv 0.x.x", congratulations, installation successful!
After installing uv, it will quietly stay on your computer, ready to help you. Next, let's use it to create a new project.
II. Creating a New Project: Specifying Python 3.10, Getting What's Missing
Subsequent commands can use the
CMD
console orPowerShell
. Directly clear the original content in the address bar of a folder, enterPowerShell
orcmd
and press Enter to open it
Suppose you want to try a certain AI project, but it requires Python 3.10, and you may not have this version on your computer. The power of uv lies in its ability to automatically download and set it up for you.
- Create a New Project Folder:
- Create a folder on your desktop or anywhere, for example, named "myai", enter the folder, and open the cmd console.
- Create a Project and Specify Python 3.10:
- Enter this command:
uv init myai --python 3.10
- uv will create a new project. If Python 3.10 is not on the computer, it will automatically download it from the Internet (may take a few minutes), and then generate a virtual environment in the folder (like an independent small room, only for this project).
- Enter this command:
- See the Results: There will be several more files in the folder, such as
pyproject.toml
(the project's "manual") and.python-version
(remembering the Python version you are using).
Now, you have a clean project environment. Let's add something to it!
III. Adding, Removing, and Updating Third-Party Modules: As Simple as Ordering Takeout
AI projects usually require some "third-party modules" (such as data processing toolkits). uv makes this as intuitive as ordering takeout.
- Add Module:
- Suppose the project needs
numpy
(a mathematical calculation tool), enter:uv add numpy
- uv will add
numpy
topyproject.toml
and download and install it into the virtual environment, done in seconds.
- Suppose the project needs
- Remove Module:
- If you don't think you need
numpy
, enter:uv remove numpy
- It will remove it from the project, cleanly.
- If you don't think you need
- Update Module:
- Want to use the latest version of
numpy
? Enter:uv add numpy --upgrade
- uv will check for the latest version and update it, saving you the trouble of checking manually.
- Want to use the latest version of
These commands will automatically keep project dependencies consistent, without worrying about version conflicts. Next, let's run a Python file to try it out.
IV. Running Python Files: One-Click Start
Suppose you wrote a simple test.py
in the project, the content is to print "Hello, AI!":
print("Hello, AI!")
Just enter:
uv run python test.py
"Hello, AI!" will appear on the screen. uv will automatically run with the Python 3.10 environment in the project, saving you the trouble of activating the environment. If the file has dependencies (such as using numpy
), uv will also ensure that they are all in place.
V. Cloning a Project from GitHub and Running It: Taking pyvideotrans as an Example
Now, let's try downloading a real project from GitHub, such as pyvideotrans
(a video translation tool, address: https://github.com/jianchang512/pyvideotrans.git
), and then configure and run it with uv.
- Clone Repository:
- First install Git (go to
https://git-scm.com/downloads/win
to download, just "Next" all the way during installation). You need to reopen PowerShell or cmd terminal after installation. - In PowerShell or cmd terminal, enter:
git clone https://github.com/jianchang512/pyvideotrans.git
- First install Git (go to
- This will download the project to the
pyvideotrans
directory under the current folder.
- Enter Project Directory:
- Enter
cd pyvideotrans
.
- Enter
- Initialize with uv and Specify Python 3.10:
- Enter:
uv init --python 3.10
- uv will check for Python 3.10 (download if not available) and create a virtual environment for the project.
- Enter:
- Install Dependencies:
- The project comes with a
requirements.txt
file, which lists the required modules. Enter:uv pip sync requirements.txt
- The project comes with a
- uv will install all dependencies according to this file, such as
torch
,requests
, etc. torch is larger and may take a long time.
- Run sp.py:
- Enter:
uv run sp.py
- If everything goes well, the project will start! You can try translating videos according to its instructions.
- Enter:
Isn't the whole process very simple? uv hides the complex dependency management behind the scenes, and you only need a few commands to complete it.
VI. uv's Major Functions and Common Commands: A Summary
uv is an all-rounder, especially suitable for people like you who like to play with GitHub projects. What's so great about it?
- Super Fast Installation: 10-100 times faster than traditional tools (such as pip).
- Environment Management: Automatically create a virtual environment, no need to manually activate, specify whichever Python version you want to use.
- Dependency Management: Supports
pyproject.toml
andrequirements.txt
, adding, removing, and updating modules is done with one click. - One-Click Run: Use
uv run
to directly run scripts, regardless of environment configuration.
Common command quick reference:
uv --version
: Check uv version.uv init project_name --python 3.10
: Create a new project and specify the Python version.uv add module_name
: Add a module.uv remove module_name
: Remove a module.uv pip sync requirements.txt
: Install modules in the dependency file.uv run file_name
: Run a Python file.