r/Python • u/dark_prophet • Mar 18 '26
Discussion How to pass command line arguments to setup.py when the project is built with the pyptoject.toml ?
4
u/Glathull Mar 18 '26
Are you actually looking for cli arguments to setup.py? Or are you wanting to pass cli args to your app’s entry point when you start it running?
36
u/denehoffman Mar 18 '26
Can you give an example of what you’re trying to do here? Generally setup.py is not needed with a pyproject.toml, and install-time cli arguments are usually frowned upon since you can’t access them easily though the Python package manager
17
u/cig-nature Mar 18 '26
Instead of that, I would make the items for CUDA support optional dependencies. Eg: pip install foo[cuda]
https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#dependencies-and-requirements
11
u/Beginning-Fruit-1397 Mar 18 '26
Setuptools is outdated. Just use UV build system. But as someone already pointed out, your question is not really clear anyway
2
u/TheBB Mar 19 '26 edited Mar 19 '26
Setuptools is not really outdated, just frowned upon I guess - being that it runs Python code in the build stage. And uv doesn't have a build system. If you use it to initialize a Python project it defaults to hatch.
Using setuptools to build a package in different configurations sounds like an entirely valid use case to me. Although I would probably go with environment variables, not CLI args.
Better yet, if possible (which is not always the case) extract the optional features in their own packages and include them as optional dependencies.
3
u/Beginning-Fruit-1397 Mar 19 '26
Well idk if there's a difference between build "system" and "backend" but I use it in all my projects: https://docs.astral.sh/uv/concepts/build-backend/
3
u/TheBB Mar 19 '26
Dang, sorry, seems I'm out of date.
2
u/neuronexmachina Mar 20 '26
To be fair, it only became non-experimental last year: https://github.com/astral-sh/uv/issues/8779
2
u/MolonLabe76 Mar 18 '26 edited Mar 20 '26
Could this be achieved with a config.yaml file? Or something similar? The user can edit the yaml file and the scripts can simply read the yaml when they run? Yaml syntax is very well suited for config settings like this. And you can use PyYAML for very easy loading. Example:
# config.yaml
use_cuda: trueimport yaml
config_path = "path/to/config.yaml"
with open(config_path, 'r') as file:
config_dict = yaml.safe_load(file)
use_cuda = config_dict["use_cuda"]
...1
u/MacShuggah Mar 19 '26
Can you use env vars instead of cli arguments? Might be your best option until you get to move away from setup.py
1
u/bryanv_ Mar 19 '26
The only reasonable solution these days is to use environment variables (or possibly a config file).
1
u/gerardwx Mar 19 '26
[project]
name = "mygeneric"
description = "TBD"
license = {text = "MIT" }
version = "1.0"
dependencies = []
requires-python= ">= 3.10"
readme = "README.md"
authors = [
{name = "Gerard"},
{email = "[email protected]"}
]
[tool.setuptools]
package-dir = {"" = "src" } # Tells setuptools that the root package is in the "src" directory
[tool.setuptools.packages.find]
where = ["src"] # Specify the "src" directory to look for packages
[project.scripts]
mygeneric = "mygeneric.main:main"
[project.optional-dependencies]
# test = ['pytest']
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
0
30
u/NeilGirdhar Mar 18 '26
Stop using setup.py.