Intro
Few days ago, I published my first python package to pypi. Here’s what I’ve learned.
Tools
build
and twine
were the two fundamental packages.
The build
package literally builds a repository into a distributable compressed file.
This simple command python3 -m build
will create a dist/
directory which contains the tar.gz
and whl
files.
twine
can directly upload your repository to pypi
and testpypi
. testpypi
is nothing more than a test version of pypi
. This comes in handy before you publish to pypi
in order to get an exact preview.
Structuring
The following is the project tree structure of my tictronome
package. Starting from the top, LICENSE
file is a simple text file that contains an open source license. The MIT license is widely used.
README.md
should contain instructions on how to install and use your package. I personally recommend creating a separate readme file for pypi
since it does not render images in my assets
folder. For convenience I just used the same README.md
file for my GitHub repo.
assests
directory contains images and resources associated with my README.md
file.
build/
is automatically created by running the build
command.
pyproject.toml
contains configurations of your build system, such as the version of setuptools
required.
setup.cfg
is the core configuration file. This will be a roadmap to your build. The .cfg
file is a static config file. However you can also use a dynamic configuration file(setup.py
). I decided to use the static configfile from this documentation.
test/
contains simple tests for my main scripts.
|
|
I used the src
directory to be my package directory. So if I do a pip install tictronome
, then I can import code in the following way.
|
|
Note that Tic
in the first line is a class defined in tictronome.py
.
Todos
Even after some trial and error, I was not able to perform a direct import. In other words, I could not do this.
|
|
Still trying to figure out how I could make this happen.
Also, on my next build, I’m considering to use a dynamic configuration file(setup.py
) and not use the build
package and only using the setuptools
package.