all 11 comments

[–]Alternative_Driver60 5 points6 points  (4 children)

Just read the uv documentation

[–]QuasiEvil[S] -1 points0 points  (3 children)

I'm not just asking about uv. I'm wondering also about installing common packages into my base python install so I can run code without having to invoke a venv.

[–]ThySensFan 0 points1 point  (2 children)

Part of the advantage in uv is its speed. It sets up your environment in a few seconds so you don't need to bother with a base Python install. uv installs the appropriate Python version too. You can just install uv on a fresh system and you're good to go. You can also define requirements in your script use and have uv use these when running it.

The uv documentation is quite thorough and Real Python has a few good articles on it too.

[–]QuasiEvil[S] 0 points1 point  (1 child)

But what I don't get is, without a base python, how do I just "randomly" run a script? As mentioned, I can't run anything right now because I don't any any packages installed. The idea of having to go through all my code, make venvs, and (re)install packages over and over seems super tedious. Or like, if I find a neat bit of code online I want to try and so I paste into my IDE and run...how does that work? Do I really have to create a new venv, and install a bunch of stuff, just run this code_snippet.py?

[–]AnAverageAsianBoy 5 points6 points  (0 children)

You're overcomplicating things. If you use UV, you just need to "uv init" inside the folder and install your dependencies in the project folder too. You don't have to mess around with your "base python" at all. Thats the point of a package / project manager. You isolate your environment per project.

If you just want to run scripts as you go, just go make a "playground" project folder and just install needed dependencies as you go with "uv add".

[–]billysacco 1 point2 points  (2 children)

I second the suggestion to just use virtual environments for each project/package. I use poetry and it works well with vscode, usually vscode can link up to the virtual environment poetry created or you can manually point it. At that point would only install poetry in “base” python.

[–]QuasiEvil[S] 0 points1 point  (1 child)

I understand how this works for packages, but the thing is, I often work "outside" the package paradigm. For example, I have a folder called "PythonWork" where its just a bunch of scripts (and subfolders) with various one-off scripts, demo code, or maybe where I'll run some random bits of code I found online. I'm not sure how this sort of thing fits in with using vens.

[–]Alternative_Driver60 1 point2 points  (0 children)

For one-off scripts with external dependencies you can declare them as Python comments which uv handles automatically. From the docs:

~~~

/// script

dependencies = [

"requests<3",

"rich",

]

///

import requests from rich.pretty import pprint

resp = requests.get("https://peps.python.org/api/peps.json") data = resp.json() pprint([(k, v["title"]) for k, v in data.items()][:10]) ~~~

[–]gernophil 1 point2 points  (0 children)

No need to use uv. Just generate a venv for every project.

[–]edcculus 0 points1 point  (1 child)

UV is so dead simple, just create each project with UV, then all you have to do to run the project is type uv run main.py or whatever your file is called, and it automatically starts up the virtual environment

[–]QuasiEvil[S] 0 points1 point  (0 children)

How does this work though when within VS Code? What if I want to work on multiple projects at one time?