Python environments: uv

satya - 6/29/2025, 8:38:36 AM

is there a diff between creating a python venv through venv or uv venv?

is there a diff between creating a python venv through venv or uv venv?

Search for: is there a diff between creating a python venv through venv or uv venv?

satya - 6/29/2025, 8:45:18 AM

My github write up on venv

My github write up on venv

satya - 6/29/2025, 8:47:29 AM

My github article: previous notes on uv

My github article: previous notes on uv

satya - 6/29/2025, 9:36:14 AM

uv venv equivalence



uv venv

//is equivalent to

python -m venv .venv

//Creates a sub directory .venv

satya - 6/29/2025, 9:37:26 AM

The -m means

  1. python a.py
  2. //runs a file anywhere in python path
  3. python -m a
  4. //runs a module in the available modules

satya - 6/29/2025, 10:02:36 AM

uv run a.py behavior

  1. if .venv exists, uses it as the dependency resolver cache
  2. if project.toml exists, resolves by installing dependencies into the .venv
  3. similar to uv pip install
  4. creates a uv.lock file in the process
  5. then runs the a.py

satya - 6/29/2025, 10:03:38 AM

In other words you can do this


//create a .venv for keeping vscode happy
uv venv

//ensure there is a project.toml and run your .py file
uv run a.py

satya - 8/1/2025, 9:27:33 AM

Understanding uv: Background of how python programs are executed

  1. Yes! it is a package manager. That postpones the discussion further...
  2. Traditionally python programs, or more like scripts are run with python.exe and the name of the python script file like a.py
  3. Any libraries that a.py uses are resolved using globally installed python libraries
  4. That causes conflicts as all scripts are sharing a common library environment
  5. This variation can go not only to the libraries but the version of python.exe itself.

satya - 8/1/2025, 1:02:03 PM

Enters venv from python

  1. Natively python answers this with a sub directory called .venv under the project root.
  2. It relies on adjusting the command line paths and settings to store all new installed libraries into that venv directory
  3. It provide a batch file called venv/activate.sh to setup the environment variables in the shell
  4. Once you activate it, all subsequent python executions will use the venv for path resolution.
  5. Especially the python pip install, will installs the subsequent libs into the venv sub directory.
  6. When you no longer wants to use the specialized enviornment in the shell you call deactivate or just close the shell.

satya - 8/1/2025, 1:08:37 PM

What is wrong with it that we need uv?

  1. At its core, you are temporarily changing the shell to simulate a virtual environment
  2. There is no explicit registry that is managed to recreate the library environment when it is transported.
  3. You typically do that by doing a pip freeze to list the total installed "libraries" in venv and check it in, usually something like a "requirements.txt", a list of all libraries
  4. That is a manual step
  5. Also you have to remember to activate and deactivate the shells
  6. It does not rely on a project definition file like your-project.toml that explicitly controls how things and dependencies are setup.
  7. In other words you can have two projects that can share that venv and there isn't a way to know which shares what
  8. So it relies on the programmer to manage that meta data

satya - 8/1/2025, 1:10:50 PM

is then python venv approach bad?

  1. It is quite workable
  2. it is simple enough
  3. Most tools like vscode assume venv and pythons approach of running python programs. They are not yet fully programmed for "uv" like behavior
  4. It works quite well
  5. The need to go to "uv" is a small enhancement (or big if a particular aspect of uv makes your life simpler)

satya - 8/1/2025, 1:11:15 PM

How big a conceptual difference is it to go to uv?

  1. Not big, a small adjustment if you understand how "uv" works.

satya - 8/1/2025, 1:19:48 PM

so how does uv work?

  1. Practically speaking you run every python script using "uv script args"
  2. This includes pip install, a.py, or any script
  3. You are telling "uv", like an agent, hey, run this python code for me!!
  4. uv then decides to run that code using the python interpreter by picking the right path and the right libraires using a config file for that project context.
  5. This project context is maintained in the root of the repo as "project.toml"
  6. This file contains the project dependencies for all the script files in that project
  7. uv will install all the dependencies from project.toml into the .venv sub directory, by creating one if it doesn't exist, the venv directory itself
  8. It does not need to set the activation
  9. It does it programmatically at run time just for that script
  10. Your shell is untouched
  11. Further it creates uv lock file that contains all the long list of resolved dependencies
  12. this file can be checked in along with project.toml into github
  13. the venv then can be recreated just using the project.toml spec
  14. In other words the meta data about your project is captured in project.toml

satya - 8/1/2025, 1:20:51 PM

What is common between venv and uv approach?

  1. both maintain .venv
  2. both resolves libraries correctly
  3. both hell in running the python scripts
  4. You can use one or the other

satya - 8/1/2025, 1:24:28 PM

What is different?

  1. uv uses project.toml to do this
  2. uv also uses uv lock file to keep a record of installed dependencies
  3. Every command is run with "uv" as the prefix or the main command

satya - 8/1/2025, 1:33:14 PM

How uv is used in python projects a procedural summary

satya - 8/1/2025, 1:36:57 PM

The things you do when you are developing with python

  1. Create a repo -- to start your project with a root directory
  2. Write python files there, .py
  3. Install desired libraries needed by the .py files
  4. Run the python file
  5. Set the shell back to its original state if needed
  6. Look at what libs are installed
  7. How to uninstall and restart?

satya - 8/1/2025, 1:37:25 PM

See how each of these steps are performed with uv or python

See how each of these steps are performed with uv or python

satya - 8/1/2025, 3:17:37 PM

Few References now

Few References now

satya - 8/1/2025, 3:18:08 PM

Github article: My first attempt at understanding uv

Github article: My first attempt at understanding uv

satya - 8/1/2025, 3:19:32 PM

it has

  1. what is it
  2. how to install
  3. how to use it
  4. More focus on using without venv
  5. Speaks of some relationships with venv
  6. ......
  7. Note: it is incomplete
  8. Especially using venv naturally
  9. Later article talks about this

satya - 8/1/2025, 3:20:39 PM

Github article 2: Second attempt

Github article 2: Second attempt

satya - 8/1/2025, 3:22:33 PM

It has

  1. Focuses on compatibility with venv
  2. Common useful commands
  3. Brings it closed to how you use uv
  4. As always references to public uv material
  5. .....
  6. Still not a comprehensive one nor a simpler one
  7. A good one will combine both

satya - 8/1/2025, 3:28:51 PM

Astral Blog ? Introducing uv

Astral Blog ? Introducing uv

satya - 8/1/2025, 3:37:13 PM

it has

  1. package manager in Rust
  2. A replacement for Rye
  3. Astral: Developer tools for the Python like Ruff, Python linter and formatter.
  4. Single binary
  5. Expected to start to look like "Cargo for Rust"

satya - 8/1/2025, 3:38:05 PM

Astral uv github page

Astral uv github page

satya - 8/1/2025, 3:39:44 PM

Astrals uv docs

Astrals uv docs

satya - 8/1/2025, 3:40:24 PM

Features at Astral uv docs

Features at Astral uv docs

satya - 8/1/2025, 3:42:39 PM

it has

  1. how to install python itself :)
  2. Run scripts
  3. Manage project.toml
  4. run global packages without installing (uvx)
  5. Full backward compatibility for venv
  6. ....
  7. All in all it is beautifully done

satya - 8/1/2025, 4:50:48 PM

Uv in pictures now....

Uv in pictures now....

satya - 8/1/2025, 4:51:24 PM

start with a non python virtual environments

satya - 8/1/2025, 4:52:51 PM

Python native virtual environments

satya - 8/1/2025, 4:54:21 PM

So, now uv features

satya - 8/1/2025, 4:56:31 PM

How uv is used, or works

satya - 8/1/2025, 4:58:00 PM

Use this link to look up Common uv commands

Common uv commands

satya - 8/1/2025, 4:58:28 PM

More references about python environments and uv

More references about python environments and uv

satya - 8/1/2025, 5:07:40 PM

Github article: One more reference: The first article on natice virtual environments

Github article: One more reference: The first article on natice virtual environments

satya - 8/1/2025, 5:08:43 PM

it has

  1. what are virtual environments
  2. in pictures
  3. how to set them up
  4. how to activate them and deactivate them
  5. hot pip install
  6. look up at what is installed
  7. References
  8. Finally, general understanding of virtual environments

satya - 8/4/2025, 1:39:42 PM

While at it a few commands

  1. uv run <script.py>
  2. uv venv
  3. uv pip install <package>
  4. uv pip uninstall <package>
  5. uv pip sync
  6. uv lock
  7. uv pip freeze
  8. uv cache dir
  9. uv cache clear
  10. uvx <tool>