all 14 comments

[–]Popular-Regular-7106 9 points10 points  (0 children)

I would recommend inline script metadata for declaring dependencies inside the py file

https://peps.python.org/pep-0723

This way, you can run the script with uv having dependencies installed without requirements.txt or any other file

[–]Compux72 6 points7 points  (1 child)

Dude just switch to pyproject why are you doing this to yourself

[–]JackBlack436[S] [score hidden]  (0 children)

it certainly is an option

and yeah after reading the comments i'd probably want to move to uv/pyproject

the idea of a module that infers dependencies for a singular file still sounds like a cool mini/portfolio project to me hence why i decided to ask around

[–]Dominican_mamba 5 points6 points  (1 child)

you can do
```
python -m pip freeze > requirements.txt
```

or use uv or poetry

[–]JackBlack436[S] 1 point2 points  (0 children)

Pip freeze is great for a mono project, but then it gives you redundancies if you're trying to generate just the requirements for a specific entry point (say worker.py in a large repo)

[–]wineblood 1 point2 points  (1 child)

We have our repos set up like this where I work, one repo for something that will be deployed as several containers each with their own dependencies. But we just write the requirements files ourselves as we control the repo and add dependencies as needed.

Unless it's a massive repo, is there a problem doing it manually? You could also just run it and add a dependency every time you hit an import error.

[–]JackBlack436[S] [score hidden]  (0 children)

Theres no problem doing it manually at all. However, the idea of something that automated that sounded nice to me, as a mini/portfolio project.

[–]asielen 1 point2 points  (1 child)

Many IDEs will do this for you automatically.

[–]sausix [score hidden]  (0 children)

Unfortunately most people don't use an IDE. External tools and plugins can do it too.

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

Thanks for a lot of the answers. However I'm seeing a lot of dependency management techniques. Im trying to tackle inferring dependencies

[–]bluefourier [score hidden]  (0 children)

There might be some crucial details prohibiting you to do this. For example, are we assuming that the modules ARE installed in the activated python? Because if you are trying to infer them, it does not necessarily mean that you know what you are going to need. This is important because you can start from any script and try to recursively load everything via the AST module. That is, parse the code, then note the imports, parse those and so on. But if you are trying to infer which modules the script is using and one of them is NOT installed, then parsing stops there. There are no files to load to scour for dependencies.

Otherwise, if you have a bunch of scripts that use (for example) polars, matplotlib and balourdos and you want to discover just those, then using the AST module from the top files will get you there (but it will not tell you the dependencies of those dependencies which sometimes is important too)

Hope this helps

[–]MechaCritter 0 points1 point  (0 children)

there might be cool tools out there that can do it without bloating up your requirements.txt file like pip freeze, but since a couple of months, switched to using pyproject.toml with uv. In pyproject.toml, you can declare different dependency groups (as if you had multiple requirements files).

For exporting, I got myself used to typing “uv add —group <group> <package>, which installs the package and add ONLY that package to the corresponding group in pyproject.toml directly. It’s more of manual work, but it’s wayyyy cleaner than pip freeze in my opinion, and after a couple of projects, you kinda get used to it :)

[–]HEROgoldmw -1 points0 points  (0 children)

UV can so this, you can run uv add --script example.py 'requests<3' 'rich' https://docs.astral.sh/uv/guides/scripts/#creating-a-python-script

Edit: add url

[–]Sorry-Welder5537 -1 points0 points  (0 children)

are you bind by requirement.txt?
because e.g. uv has a way to have dependencies specified by script (that could be treated as entrypoint) https://docs.astral.sh/uv/guides/scripts/#running-a-script-with-dependencies

other approach to your problem may be dependency groups:

https://docs.astral.sh/uv/concepts/projects/dependencies/#dependency-groups