Return to Homepage

Return to Notes list

2023-12-28

Make anything installable with pipx

pipx is probably the only sane way to install a python package as a system utility. For projects that don’t provide wheels, do the following

check out the project’s repository:

git clone some_url

Most python projects have a file structure something like this:

.
├── LICENSE
├── README.md
└── project_name
    ├── __init__.py
    ├── __main__.py
    ├── project_code_1.py
    ├── project_code_2.py
    └── et_cetera.py

run poetry init and click through most of the options. The only thing we care about is defining the project dependencies. If no requirements.txt file is given, you’ll have to define the dependencies manually. Otherwise, run:

xargs poetry add < requirements.txt

Assuming that __main__.py defines a main function which is then exported by __init__.py, add the following to your new pyproject.toml file:

[tool.poetry.scripts]
project_name = 'project_name:main'

Now build and install the project:

poetry build
pipx install dist/*.whl  # may need the --force option if reinstalling

Done!