This article is converted by ็ฎๆฆ SimpRead, original source v2fy.com
Managing Python Projects More Quickly Using uv
Views: 34
For Python beginners, as long as you master the two commands pip (to manage dependencies) and python (to run code), you can write and execute scripts with very little code;
However, from an engineering perspective, each projectโs Python version and corresponding dependency versions should be isolated from each other. To solve the issue of Python project engineering, uv was created.
Installing UV
- Installation commands for macOS and Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
- Windows version
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
![]()
Creating a Project with Python3.13
mkdir yiyan_requests
cd yiyan_requests
uv python install 3.13
uv init -p 3.13
At this point, two important files main.py and pyproject.toml will be generated under the yiyan_requests folder.
![]()
The project file pyproject.toml contains
[project]
name = "yiyan-requests"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13"
dependencies = []
The code test file main.py contains
def main():
print("Hello from yiyan-requests!")
if __name__ == "__main__":
main()
Running main.py, you can see that Python version 3.13 is used and the content is output correctly.
uv run main.py
![]()
We install a requests library and complete an API request.
# Install requests
uv add requests
# To uninstall requests, run uv remove requests
After installation, we will find that the projectโs .venv directory already contains the storage path for the requests dependency.
โ yiyan_requests git:(master) โ tree -a
.
โโโ .git
โ โโโ branches
โ โโโ config
โ โโโ description
โ โโโ HEAD
โ โโโ hooks
โ โ โโโ applypatch-msg.sample
โ โ โโโ commit-msg.sample
โ โ โโโ fsmonitor-watchman.sample
โ โ โโโ post-update.sample
โ โ โโโ pre-applypatch.sample
โ โ โโโ pre-commit.sample
โ โ โโโ pre-merge-commit.sample
โ โ โโโ prepare-commit-msg.sample
โ โ โโโ pre-push.sample
โ โ โโโ pre-rebase.sample
โ โ โโโ pre-receive.sample
โ โ โโโ push-to-checkout.sample
โ โ โโโ sendemail-validate.sample
โ โ โโโ update.sample
โ โโโ info
โ โ โโโ exclude
โ โโโ objects
โ โ โโโ info
โ โ โโโ pack
โ โโโ refs
โ โโโ heads
โ โโโ tags
โโโ .gitignore
โโโ main.py
โโโ pyproject.toml
โโโ .python-version
โโโ README.md
โโโ uv.lock
โโโ .venv
โโโ bin
โ โโโ activate
โ โโโ activate.bat
โ โโโ activate.csh
โ โโโ activate.fish
โ โโโ activate.nu
โ โโโ activate.ps1
โ โโโ activate_this.py
โ โโโ deactivate.bat
โ โโโ normalizer
โ โโโ pydoc.bat
โ โโโ python -> /home/parallels/.local/share/uv/python/cpython-3.13.2-linux-aarch64-gnu/bin/python3.13
โ โโโ python3 -> python
โ โโโ python3.13 -> python
โโโ CACHEDIR.TAG
โโโ .gitignore
โโโ lib
โ โโโ python3.13
โ โโโ site-packages
โ โโโ certifi
โ โ โโโ cacert.pem
โ โ โโโ core.py
โ โ โโโ __init__.py
โ โ โโโ __main__.py
โ โ โโโ py.typed
โ โโโ certifi-2025.1.31.dist-info
โ โ โโโ INSTALLER
โ โ โโโ LICENSE
โ โ โโโ METADATA
โ โ โโโ RECORD
โ โ โโโ REQUESTED
โ โ โโโ top_level.txt
โ โ โโโ WHEEL
โ โโโ charset_normalizer
โ โ โโโ api.py
โ โ โโโ cd.py
โ โ โโโ cli
โ โ โ โโโ __init__.py
โ โ โ โโโ __main__.py
โ โ โโโ constant.py
โ โ โโโ __init__.py
โ โ โโโ legacy.py
โ โ โโโ __main__.py
โ โ โโโ md.cpython-313-aarch64-linux-gnu.so
โ โ โโโ md__mypyc.cpython-313-aarch64-linux-gnu.so
โ โ โโโ md.py
โ โ โโโ models.py
โ โ โโโ py.typed
โ โ โโโ utils.py
โ โ โโโ version.py
โ โโโ charset_normalizer-3.4.1.dist-info
โ โ โโโ entry_points.txt
โ โ โโโ INSTALLER
โ โ โโโ LICENSE
โ โ โโโ METADATA
โ โ โโโ RECORD
โ โ โโโ REQUESTED
โ โ โโโ top_level.txt
โ โ โโโ WHEEL
โ โโโ idna
โ โ โโโ codec.py
โ โ โโโ compat.py
โ โ โโโ core.py
โ โ โโโ idnadata.py
โ โ โโโ __init__.py
โ โ โโโ intranges.py
โ โ โโโ package_data.py
โ โ โโโ py.typed
โ โ โโโ uts46data.py
โ โโโ idna-3.10.dist-info
โ โ โโโ INSTALLER
โ โ โโโ LICENSE.md
โ โ โโโ METADATA
โ โ โโโ RECORD
โ โ โโโ REQUESTED
โ โ โโโ WHEEL
โ โโโ __pycache__
โ โ โโโ _virtualenv.cpython-313.pyc
โ โโโ requests
โ โ โโโ adapters.py
โ โ โโโ api.py
โ โ โโโ auth.py
โ โ โโโ certs.py
โ โ โโโ compat.py
โ โ โโโ cookies.py
โ โ โโโ exceptions.py
โ โ โโโ help.py
โ โ โโโ hooks.py
โ โ โโโ __init__.py
โ โ โโโ _internal_utils.py
โ โ โโโ models.py
โ โ โโโ packages.py
โ โ โโโ sessions.py
โ โ โโโ status_codes.py
โ โ โโโ structures.py
โ โ โโโ utils.py
โ โ โโโ __version__.py
โ โโโ requests-2.32.3.dist-info
โ โ โโโ INSTALLER
โ โ โโโ LICENSE
โ โ โโโ METADATA
โ โ โโโ RECORD
โ โ โโโ REQUESTED
โ โ โโโ top_level.txt
โ โ โโโ WHEEL
โ โโโ urllib3
โ โ โโโ _base_connection.py
โ โ โโโ _collections.py
โ โ โโโ connectionpool.py
โ โ โโโ connection.py
โ โ โโโ contrib
โ โ โ โโโ emscripten
โ โ โ โ โโโ connection.py
โ โ โ โ โโโ emscripten_fetch_worker.js
โ โ โ โ โโโ fetch.py
โ โ โ โ โโโ __init__.py
โ โ โ โ โโโ request.py
โ โ โ โ โโโ response.py
โ โ โ โโโ __init__.py
โ โ โ โโโ pyopenssl.py
โ โ โ โโโ socks.py
โ โ โโโ exceptions.py
โ โ โโโ fields.py
โ โ โโโ filepost.py
โ โ โโโ http2
โ โ โ โโโ connection.py
โ โ โ โโโ __init__.py
โ โ โ โโโ probe.py
โ โ โโโ __init__.py
โ โ โโโ poolmanager.py
โ โ โโโ py.typed
โ โ โโโ _request_methods.py
โ โ โโโ response.py
โ โ โโโ util
โ โ โ โโโ connection.py
โ โ โ โโโ __init__.py
โ โ โ โโโ proxy.py
โ โ โ โโโ request.py
โ โ โ โโโ response.py
โ โ โ โโโ retry.py
โ โ โ โโโ ssl_match_hostname.py
โ โ โ โโโ ssl_.py
โ โ โ โโโ ssltransport.py
โ โ โ โโโ timeout.py
โ โ โ โโโ url.py
โ โ โ โโโ util.py
โ โ โ โโโ wait.py
โ โ โโโ _version.py
โ โโโ urllib3-2.3.0.dist-info
โ โ โโโ INSTALLER
โ โ โโโ licenses
โ โ โ โโโ LICENSE.txt
โ โ โโโ METADATA
โ โ โโโ RECORD
โ โ โโโ REQUESTED
โ โ โโโ WHEEL
โ โโโ _virtualenv.pth
โ โโโ _virtualenv.py
โโโ lib64 -> lib
โโโ pyvenv.cfg
34 directories, 162 files
Modify the code in main.py to
import requests
import json
# Send a GET request to the specified URL
response = requests.get("https://v1.hitokoto.cn/")
# Check if the request was successful
if response.status_code == 200:
# Parse the returned data into JSON format
result = response.json()
# Pretty-print the JSON data
print(json.dumps(result, ensure_ascii=False, indent=4))
else:
print(f"Request failed, status code: {response.status_code}")
Run the code
uv run main.py
![]()
uv can automatically set the activation timing of the virtual environment Python interpreter
Run uv run which python to check the current Python interpreter location. From the image below, we can see that if you leave the project directory, running uv run which python will automatically point to the global Python interpreter /home/parallels/.local/share/uv/python/cpython-3.13.2-linux-aarch64-gnu/bin/python. When entering the project directory, running uv run which python will point to the Python interpreter in the projectโs own directory home/parallels/yiyan_requests/.venv/bin/python.
![]()
How to manage an old Python project with requirements.txt using uv?
Take the currently popular OpenManus as an example
# Get the project code
git clone https://github.com/mannaandpoem/OpenManus.git
# Enter the project directory
cd OpenManus
# Specify the Python interpreter version for the project
uv venv --python 3.12
# Activate the virtual environment
source .venv/bin/activate # On Unix/macOS
# Windows activation of virtual environment
# .venv\Scripts\activate
# Install dependencies from requirements.txt
uv pip install -r requirements.txt
We do not need to run source .venv/bin/activate to activate the environment every time we enter the directory. After completing the project setup with uv venv --python 3.12 (which creates a .venv folder in the project directory), every time you enter the project directory, it will automatically activate the corresponding Python interpreter (the interpreter under the .venv directory).
![]()
How to generate requirements.txt
uv pip compile pyproject.toml -o requirements.txt
The generated output
โ yiyan_requests git:(master) โ ls -la
total 40
drwxrwxr-x 4 parallels parallels 4096 Mar 17 14:38 .
drwxr-x--- 22 parallels parallels 4096 Mar 17 15:57 ..
drwxrwxr-x 7 parallels parallels 4096 Mar 17 14:38 .git
-rw-rw-r-- 1 parallels parallels 109 Mar 17 14:38 .gitignore
-rw-rw-r-- 1 parallels parallels 403 Mar 17 14:42 main.py
-rw-rw-r-- 1 parallels parallels 185 Mar 17 14:38 pyproject.toml
-rw-rw-r-- 1 parallels parallels 5 Mar 17 14:38 .python-version
-rw-rw-r-- 1 parallels parallels 0 Mar 17 14:38 README.md
-rw-rw-r-- 1 parallels parallels 7268 Mar 17 14:38 uv.lock
drwxrwxr-x 4 parallels parallels 4096 Mar 17 14:38 .venv
โ yiyan_requests git:(master) โ uv pip compile pyproject.toml -o requirements.txt
Resolved 5 packages in 1.95s
# This file was autogenerated by uv via the following command:
# uv pip compile pyproject.toml -o requirements.txt
certifi==2025.1.31
# via requests
charset-normalizer==3.4.1
# via requests
idna==3.10
# via requests
requests==2.32.3
# via yiyan-requests (pyproject.toml)
urllib3==2.3.0
# via requests
โ yiyan_requests git:(master) โ ls -la
total 44
drwxrwxr-x 4 parallels parallels 4096 Mar 17 15:57 .
drwxr-x--- 22 parallels parallels 4096 Mar 17 15:57 ..
drwxrwxr-x 7 parallels parallels 4096 Mar 17 14:38 .git
-rw-rw-r-- 1 parallels parallels 109 Mar 17 14:38 .gitignore
-rw-rw-r-- 1 parallels parallels 403 Mar 17 14:42 main.py
-rw-rw-r-- 1 parallels parallels 185 Mar 17 14:38 pyproject.toml
-rw-rw-r-- 1 parallels parallels 5 Mar 17 14:38 .python-version
-rw-rw-r-- 1 parallels parallels 0 Mar 17 14:38 README.md
-rw-rw-r-- 1 parallels parallels 324 Mar 17 15:57 requirements.txt
-rw-rw-r-- 1 parallels parallels 7268 Mar 17 14:38 uv.lock
drwxrwxr-x 4 parallels parallels 4096 Mar 17 14:38 .venv
โ yiyan_requests git:(master) โ cat requirements.txt
# This file was autogenerated by uv via the following command:
# uv pip compile pyproject.toml -o requirements.txt
certifi==2025.1.31
# via requests
charset-normalizer==3.4.1
# via requests
idna==3.10
# via requests
requests==2.32.3
# via yiyan-requests (pyproject.toml)
urllib3==2.3.0
# via requests
How to check the installed Python versions
uv python list
![]()
Summary
A popular programming language needs standardized and practical engineering tools. To achieve multi-project Python isolation, we have gone through virtualenv, pipenv, poetry, uv, and other popular libraries. The speed of project construction has become faster and the level of integration higher. With the development of AI programming, project initialization processes will tend to favor tools with better integration. New tools will be compatible with old configuration files, and less user-friendly tools will eventually fade into history.