Make your package pip installable

Here’s how to publish your package to pypi.org

sundaycrunk
2 min readOct 18, 2021

Install Poetry

See here to install poetry.

poetry new xyz OR poetry init

…with xyz being the name of your project, or having cd’d into your directory before calling init.

This will create a pyproject.toml file.

Make your project folder look like this, with main.py being the script you want to run.

xyz/
├── xyz/
│ ├── __init__.py
│ └── main.py
├── pyproject.toml
└── README.rst

Delete the README.rst and replace with a README.md file.

Make a Shell Script Variable

In the pyproject.toml file, add

[tool.poetry.scripts]
xyz = “xyz.main:start”

This says: “whenever the xyz command is typed in Terminal, from the xyz folder > main.py file, call the start function.”

Your completed pyproject.toml should look like this :

[tool.poetry]
name = "xyz"
version = "0.1.0"
description = "description of your package."
authors = ["<your_name> <your_email@domain.com>"]
readme = "README.md"
license = "MIT"
[tool.poetry.scripts]
xyz = "xyz.main:start"
[tool.poetry.dependencies]
python = "^3.9"
...
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

To complete and publish to pypi.org, run:

poetry publish

Now anyone can install your package using:

pip install xyz

And they can run it straight from terminal using:

xyz ...

Handling Arguments

If your script takes an argument to process and return data, you could either use argparse or sys.argv[1].

--

--

No responses yet