aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Programming
  • Software
  • Technology
  • Tools

How To Package Your Python Code

  • Aelia Vita
  • November 22, 2021
  • 3 minute read

Use setuptools to deliver Python code to users.

You’ve spent weeks perfecting your code. You’ve tested it and sent it to some close developer friends for quality assurance. You’ve posted all the source code on your personal Git server, and you’ve received helpful bug reports from a few brave early adopters. And now you’re ready to make your Python code available to the world.

And that’s when it hits you. You have no idea how to deliver the product.Delivering code to its target is a big deal. It’s a whole branch of software development, it’s the “D” in CI/CD, and yet many people forget all about, at least until the end. I’ve written articles about Autotools and Cmake, but some languages have their own methods to help you make your code readily available to users. For Python, a common way to deliver code to users is with setuptools.


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

Install setuptools

The easiest way to install and update setuptools is with pip:

<span class="co4">$ </span><span class="kw2">sudo</span> python <span class="re5">-m</span> pip <span class="kw2">install</span> <span class="re5">--upgrade</span> setuptools

Example library

Create a simple Python library called myhellolib for some example code in need of packaging. This library accepts a string and then prints the string in capital letters.
It’s two lines of code, but project structure is important, so first create the directory tree:

<span class="co4">$ </span><span class="kw2">mkdir</span> <span class="re5">-p</span> myhellolib.git<span class="sy0">/</span>myhellolib

To confirm that this project is an importable library (a Python “module”), create the empty file __init__.py in the code directory, along with the file that contains the code:

$ touch myhellolib.git/myhellolib/__init__.py
$ touch myhellolib.git/myhellolib/myhellolib.py

In the myhellolib.py file, enter this simple Python code:

def greeter(s):
print(s.upper())

That’s the library written.

Read More  What To Expect From Apple’s WWDC 2023

Test it

Before packaging it up, test your library. Create a myhellolib.git/test.py file and enter this code:

import myhellolib.myhellolib as hello

hello.greeter(“Hello Opensource.com.”)

Run the script:

$ cd myhellolib.git
$ python ./test.py
HELLO OPENSOURCE.COM

It works, so now you can package it up.

Setuptools

To package a project with setuptools, you must create a .toml file identifying setuptools as the build system. Place this text in a file called myhellolib.toml in your project directory:

[build-system] requires = [“setuptools”, “wheel”] build-backend = “setuptools.build_meta”

Next, create a file called setup.py, containing metadata about your project:

from setuptools import setup

setup(
name=‘myhellolib’,
version=‘0.0.1’,
packages=[‘myhellolib’],
install_requires=[
‘requests’,
‘importlib; python_version == “3.8”‘,
],
)

Believe it or not, that’s all the setup setuptools requires. Your project is ready for packaging.

Packaging Python

To create your Python package, you need a builder. A common tool is build, which you can install with pip:

<span class="co4">$ </span>python <span class="re5">-m</span> pip <span class="kw2">install</span> build <span class="re5">--user</span>

Build your project:

<span class="co4">$ </span>python <span class="re5">-m</span> build

After a few moments, the build completes, and there’s a new directory in your project folder called dist. This folder contains a .tar.gz and a .whl file.
Your very first Python package! Here’s what each one contains:

$ tar –list –file dist/myhellolib-0.0.1.tar.gz
myhellolib-0.0.1/
myhellolib-0.0.1/PKG-INFO
myhellolib-0.0.1/myhellolib/
myhellolib-0.0.1/myhellolib/__init__.py
myhellolib-0.0.1/myhellolib/myhellolib.py
myhellolib-0.0.1/myhellolib.egg-info/
myhellolib-0.0.1/myhellolib.egg-info/PKG-INFO
myhellolib-0.0.1/myhellolib.egg-info/SOURCES.txt
myhellolib-0.0.1/myhellolib.egg-info/dependency_links.txt
myhellolib-0.0.1/myhellolib.egg-info/requires.txt
myhellolib-0.0.1/myhellolib.egg-info/top_level.txt
myhellolib-0.0.1/setup.cfg
myhellolib-0.0.1/setup.py$ unzip -l dist/myhellolib-0.0.1-py3-none-any.whl
Archive:  dist/myhellolib-0.0.1-py3-none-any.whl
Name
—-
myhellolib/__init__.py
myhellolib/myhellolib.py
myhellolib-0.0.1.dist-info/METADATA
myhellolib-0.0.1.dist-info/WHEEL
myhellolib-0.0.1.dist-info/top_level.txt
myhellolib-0.0.1.dist-info/RECORD
——-
6 files

Making it available

Now that you know how easy it is to package up your Python package, you can either automate the process using Git hooks, GitLab webhooks, Jenkins, or a similar automation tool. You can even upload your project to PyPi, the popular repository for Python modules. Once it’s on PyPi, users can install it using pip, the same way you installed setuptools and build for this article!

Read More  Some Beans And Gems, Some Snakes And Elephants, With Java 17, Ruby 3, Python 3.10 And PHP 8.1 In App Engine And Cloud Functions

It’s not often the first thing you think about when sitting down to develop an application or library, but packaging code is an important aspect of programming. Python developers put a lot of thought into how programmers can make their work available to the world, and it doesn’t get much easier than setuptools. Try it out, use it, and keep coding in Python!

This feature was republished from Opensource.

For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

Aelia Vita

Related Topics
  • Cmake
  • Open Source
  • Programming
  • Python
  • Python library
You May Also Like
View Post
  • Technology

Building secure, scalable AI in the cloud with Microsoft Azure

  • July 5, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

Turns out OpenAI is the customer behind Oracle’s mysterious $30 billion cloud deal

  • July 3, 2025
aster-cloud-erp-bill_of_materials_2
View Post
  • Software
  • Software Engineering

What is an SBOM (software bill of materials)?

  • July 2, 2025
aster-cloud-sms-pexels-tim-samuel-6697306
View Post
  • Programming
  • Software

Send SMS texts with Amazon’s SNS simple notification service

  • July 1, 2025
aster-cloud-website-pexels-goumbik-574069
View Post
  • Programming
  • Software

Host a static website on AWS with Amazon S3 and Route 53

  • June 27, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

A looming hyperscaler exodus? UK IT leaders are thinking of ditching US cloud providers – here’s why

  • June 26, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

Prioritize security from the edge to the cloud

  • June 25, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

6 edge monitoring best practices in the cloud

  • June 25, 2025

Stay Connected!
LATEST
  • 1
    Building secure, scalable AI in the cloud with Microsoft Azure
    • July 5, 2025
  • 2
    Turns out OpenAI is the customer behind Oracle’s mysterious $30 billion cloud deal
    • July 3, 2025
  • aster-cloud-erp-bill_of_materials_2 3
    What is an SBOM (software bill of materials)?
    • July 2, 2025
  • aster-cloud-sms-pexels-tim-samuel-6697306 4
    Send SMS texts with Amazon’s SNS simple notification service
    • July 1, 2025
  • Camping 5
    The Summer Adventures : Camping Essentials
    • June 27, 2025
  • aster-cloud-website-pexels-goumbik-574069 6
    Host a static website on AWS with Amazon S3 and Route 53
    • June 27, 2025
  • 7
    A looming hyperscaler exodus? UK IT leaders are thinking of ditching US cloud providers – here’s why
    • June 26, 2025
  • Prioritize security from the edge to the cloud
    • June 25, 2025
  • 6 edge monitoring best practices in the cloud
    • June 25, 2025
  • Genome 10
    AlphaGenome: AI for better understanding the genome
    • June 25, 2025
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • 1
    There’s a ‘cloud reset’ underway, and VMware Cloud Foundation 9.0 is a chance for Broadcom to pounce on it
    • June 17, 2025
  • 2
    ‘This was a very purposeful strategy’: Pure Storage unveils Enterprise Data Cloud in bid to unify data storage, management
    • June 18, 2025
  • 3
    Pure Accelerate 2025: All the news and updates live from Las Vegas
    • June 18, 2025
  • Oracle adds xAI Grok models to OCI
    • June 17, 2025
  • What is cloud bursting?
    • June 18, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.