banner



How To Install Packages Using Pip

Acquire how Python code should exist packaged for PyPI

Photo by Lawless Capture on Unsplash

The most powerful feature of Python is its customs. Almost every use-example out there has a parcel built specifically for it.

Need to ship mobile/electronic mail alerts? pip install knockknock — Build ML apps? pip install streamlit — Bored of your terminal? pip install colorama — It's too easy!

I know this is obvious, but those libraries didn't magically announced. For each package, there is a person, or many persons — that actively developed and deployed that package.

Every single one.

All 300K+ of them.

That is why Python is Python, the level of support is astounding — mindblowing.

In this article, we volition learn how to build our own packages. And add together them to the Python Parcel Alphabetize (PyPI). Afterward, nosotros volition exist able to install our packages using pip install! Let's get started.

What to Build?

The start step is to figure out what nosotros should build. One time we have an idea, head over to PyPI and search for the same parcel, does information technology exist? If so, does your idea offer annihilation new or better?

One time we've got something unique, we need to double-check that our package proper noun isn't already taken.

In this article, we're going to be building a package chosen aesthetic_ascii — which volition produce random synthwave-inspired ASCII fine art. A fairly unique idea, although perhaps not too useful.

Directory Setup

We need to create a new directory that volition contain our package and deed every bit our parcel proper noun (aesthetic_ascii).

Within this directory, we have our __init__.py file which initializes our directory as a Python module— and becomes the top-level script for the module.

We can add sub-modules by add subdirectories inside the package directory and adding __init__.py files within those too.

For example, nosotros know that we tin write from bone import path. The directory structure for this would look something like this:

            os/
LICENSE
MANIFEST.in
pyproject.toml
README.physician
setup.cfg
...
os/
__init__.py
...
path/
__init.py__
...

We'll cover the other files in hither before long. But first, we need to sympathise what __init__.py is really doing.

__init__.py

At its core __init__.py, marks a directory equally a Python bundle. So, we include the top-level __init__.py file which marks the directory as our 'package'.

We then include the actual code files (or modules) alongside our __init__.py files. So, if nosotros had this structure:

                          aesthetic_ascii/              
*all config files*
aesthetic_ascii/
__init__.py
synthesize.py

Later installing the package, we would import the synthesize module like so:

            from aesthetic_ascii import synthesize          

We'll utilise this construction throughout the commodity.

Configuration Files

setup.cfg

Aslope our lawmaking files, we need several configuration files. The most important for configuring our parcel is the setup.cfg file.

This file is critical, it is our setup configuration file. During the pip install, this file tells pip how to install our parcel.

To declare that we are using setuptools to bundle our project, we first create a pyproject.toml file in our top-level directory containing:

So we create our setup.cfg file. For aesthetic_ascii, it looks like this:

There's a lot going on here, simply each detail is quite descriptive — and so I won't depict each, yet, there are a few that are less clear.

  • long_description — the parameter name tells us what this is, and nosotros tin provide unlike file formats here. Many packages stick with markdown though, and that is what we will be using — as specified in long_description_content_type.
  • classifiers — a set of identifiers that PyPI will use to categorize our package, we can discover a full list of valid classifiers here.
  • packages — a listing of dependency packages required by our package to run.
  • python_requires — Python version requirements for the library.
  • include_package_data — whether to include additional files as defined in MANIFEST.in — more than on this after.

LICENSE

Adjacent upward is our license, a very of import part of the package — even if you lot don't care who uses it, why they're using it, and whether they're making money from information technology.

We include a license to remove whatsoever shred of doubt for users of the package. Choosing a license can be very easy, but copy and paste the almost suitable text from hither — and remember to update whatever custom parts!

README.md

This file acts equally the long_description of our package — which nosotros set up upward in the setup.cfg file.

Additionally, back in our setup.cfg — we specified in long_description_content_type that nosotros would be using a text/markdown file — and so, we employ a markdown text file!

Adding Resources

We've set up our configuration files, but we're missing 1 affair — our images that volition exist used when generating random ASCII art.

These images could be any type of file that our parcel relies on. From Python 3.7 we deal with this by using the importlib.resources module.

First, we need to import this into our code and update the office of our lawmaking reading these files to utilise importlib.resource:

The commencement argument of both functions refers to the directory that the files are stored within. Additionally, any resources must exist stored in a directory that contains a __init__.py file.

And then, in our case, we demand to shop all image files within the aesthetic_ascii subdirectory:

The aesthetic_ascii/aesthetic_ascii directory includes all of our prototype files and our font file — aslope the __init__.py and synthesize.py module

Once this is all in place, we must make sure our image files will be included in the bundle — which we do via the MANIFEST.in file:

And then we brand sure the MANIFEST.in file is added to setup.cfg:

            ...
[options]
include_package_data = True
...

And we're good to go!

Local Install

Earlier uploading our packet to PyPI we can confirm that our package can be installed via pip install by navigating to our packet directory and entering:

            pip install .          

This should so install our parcel like any other package install via pip:

Screenshot of the local pip install . test

And we tin can then import our package like we would any other:

Case usage of the aesthetic_ascii package, at present installed

Build

Once we've written our lawmaking files, setup configuration, and tested the install — we're ready to build our package distribution.

The build process creates a new directory dist which will comprise a .tar.gz and .whl file — this is what we need to publish our bundle to PyPI.

To build our dist files, we use a tool creatively named build. Kickoff, we pip install build, then, while in our packet directory — type:

            python -m build          

Once this is complete, we should find a new /dist directory within our parcel directory.

Publish to TestPyPI

Finally, nosotros are ready to publish our new Python package! Again, we make use of another bundle called twine. We install this with:

            pip install twine          

Once installed, we upload to TestPyPI — a 'test' version of PyPI so that we can double-check that we have set everything up correctly. We do this past typing:

            python -grand twine upload --repository testpypi dist/*          

At this point, we'll need to login to TestPyPI — if you lot don't have an account, sign up for i here. If everything is set upwards correctly, our packet will be uploaded:

Successful upload process for aesthetic_ascii on test PyPI

Now, we can examination that our new package works through another pip install — but this time from TestPyPI:

            pip install -i https://exam.pypi.org/simple/ aesthetic-ascii          

(If you find that the parcel is already installed — just pip uninstall aesthetic-ascii).

Installing artful-ascii via TestPyPI

PyPI

Once we've confirmed that the package works — we have our concluding pace, publishing to PyPI. Over again, yous'll need to annals hither.

Next, we upload our package to PyPI with:

            python -m twine upload --repository pypi dist/*          

Uploading our package to PyPI

And nosotros're done!

That's our first Python package deployment — it's surprisingly straightforward. We can go ahead and pip install aesthetic_ascii to use the package.

A few of our A E S T H East T I C ascii masterpieces

The full code including setup/configuration files tin be found on GitHub hither.

I promise y'all've enjoyed the article. Let me know if you take whatsoever questions or suggestions via Twitter or in the comments below. If you're interested in more than content similar this, I post on YouTube as well.

Cheers for reading!

Source: https://towardsdatascience.com/how-to-package-your-python-code-df5a7739ab2e

Posted by: fletchermatelike.blogspot.com

0 Response to "How To Install Packages Using Pip"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel