user5813071
user5813071

Reputation:

What is the proper way to organize a PIP package?

I have 4 files: main.py, helper.py, clf.pkl, and tests.py.

Main.py has core classes. It needs to import helper.py for some methods and clf.pkl for data.

What is the minimal structure I can have for a Python library with 4 files?

Upvotes: 1

Views: 213

Answers (1)

Julien Palard
Julien Palard

Reputation: 11646

I would use a package to hold your files, along with a pyproject.toml to describe your project, like this:

.
├── pyproject.toml
├── MANIFEST.in
├── your_package_name
│   ├── __main__.py
│   ├── helper.py
│   └── __init__.py
└── tests
    └── tests.py

In your __init__.py file write at least:

"""A short description of your project"""
__version__ = "0.0.1"

(Change description and version accordingly).

To create your pyproject.toml you can use flit init:

pip install flit
flit init

Name your entry point __main__.py in the package so you can run it using:

python -m your_package_name

(Yes it's still a good idea to use an if __name__ == "__main__": in it, so you can import your main from your tests if needed).

You can import helper.py from __main__.py using:

from your_package_name import helper

or:

from . import helper

(I prefer the first one but I don't know if there a concensus.)

For your clf.pkl to be included in your package you'll need to create a MANIFEST.in with:

include your_package_name/clf.pkl

Your pkl will be available at:

os.path.join(os.path.dirname(os.path.abspath(__file__)), "clf.pkl")

To test it use flit install -s and to publish it on PyPI flit publish.

Upvotes: 1

Related Questions