Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the preferred way to develop a Python package without using setup.py?

I am developing a Python package, and I don't want to have to keep running pip install . to reinstall my package every time I change something. Using the -e or --editable doesn't seem to work unless I have a setup.py file, nor does --no-use-pep517. I have a pyproject.toml instead, as is preferred nowadays if I am not mistaken. So, what is the preferred way to do this nowadays?

My package is just a CLI script, but it imports some functions from another file in the same directory called utils.py. When developing, I can't just run the script manually from the terminal, because then I get "name_of_package is not a package" from the line from name_of_package.utils import function. Whereas if I just have from utils import function, I can run the script from the terminal, but when I pip install it, it says "there is no module named utils".

I did install poetry and installed my dependencies, ran poetry shell and then tried to run my script with poetry run /path/to/script.py, but I kept getting an error that my package wasn't a package.

like image 706
Christoffer Corfield Aakre Avatar asked Dec 31 '25 06:12

Christoffer Corfield Aakre


1 Answers

Nowadays, with setuptools as a build back-end you have the choice of using setup.py, setup.cfg, pyproject.toml, or any combination of these three files to declare the project's metadata and configure the project's packaging. With pyproject.toml being the preferred one as it is standardized (see below). All these are compatible with editable installation (python -m pip install --editable .).

The editable installation mechanism has a standard specification (initiated by the PEP 660 document). And many build back-ends besides setuptools support it. These are the build back-ends I know of that support the standardized editable installation mechanism:

  • enscons
  • flit-core (see flit)
  • hatchling (see hatch)
  • maturin
  • meson-python
  • pdm-backend (see pdm)
  • poetry-core (see poetry)
  • setuptools
  • whey

All these build back-ends can have their configuration in pyproject.toml (including support for the standardized [build-system] section), and all but Poetry support the standardized [project] section. I have a comparison table here.

References:

  • Is setup.py deprecated?
  • How to modernize a setup.py based project?
  • Writing your pyproject.toml
like image 57
sinoroc Avatar answered Jan 02 '26 18:01

sinoroc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!