• Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
AnonyViet - English Version
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
AnonyViet - English Version
No Result
View All Result

How to package Python code

AnonyViet by AnonyViet
January 25, 2023
in Tips
0

In this article, I will show you how to encapsulate the code Python. You spent weeks perfecting your code. You have tested it and sent it to the tester for quality assurance. You’ve posted all the source code to your personal Git server, and you’ve received helpful bug reports from some of the early users. And now you’re ready to give your Python code to the world.

Join the channel Telegram of the AnonyViet 👉 Link 👈

How to package Python code

And that’s when you realise. You do not know how to deliver the product. Providing code to people in need is a big deal. It’s an entire branch of software development, that’s the “D” in CI/CD, but many people forget. You can use Autotools and Cmake, but some other languages ​​have their own methods to help you make your code available to users. For Python, a common way to provide code to users is to use setuptools.

Easiest way to install and update setuptools is to use pip:

sudo python -m pip install --upgrade setuptools

Create an example library

Created a simple Python library called MyHellolib for some example code that needed packaging. This library takes a string and then prints the string in capital letters.

It’s two lines of code, but the project structure is important, so let’s create the directory tree first:

mkdir -p myhellolib.git/myhellolib

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

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

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

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

That’s the library part.

Check out the library

Before packaging it, check your library. Create the file myhellolib.git/test.py 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.

To package a project with setuptools, you must create a .toml file that defines setuptools as the build system. Put this 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, which contains information about your project:

from setuptools import setup

setup(
    name="myhellolib",
    version='0.0.1',
    packages=['myhellolib'],
    install_requires=[
        'requests',
        'importlib; python_version == "3.8"',
    ],
)

That’s all setuptools requires. Your project is ready to be packaged.

Packing code

To create your Python package, you need a builder. One popular tool is i, you can install it using pip:

python -m pip install build --user

Build your project:

python -m build

After a few minutes the build is complete and there is a new folder in your project folder called dist. This folder contains .tar.gz and .whl files.

Here’s your first Python package:

$ 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

Upload the packaged code

Now that you know how easy it is to package your Python code, you can 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.

This is not the first thing that comes to mind when developing an application or library, but coding is an important aspect of programming. And I think setuptools is the simplest way to package your Python code.

The article achieved: 5/5 – (100 votes)

Tags: CodepackagePython
Previous Post

The Complete NextCloud User Guide

Next Post

Lesson 262: Goal Seek in Excel

AnonyViet

AnonyViet

Related Posts

Instructions for receiving Lovable Pro 12 months for free
Tips

Instructions for receiving Lovable Pro 12 months for free

May 29, 2026
Instructions for receiving 1 month of ElevenLabs Creator for free worth
Tips

Instructions for receiving 1 month of ElevenLabs Creator for free worth $22

May 25, 2026
How to create beautiful studio-like preschool graduation photos for your child
Tips

How to create beautiful studio-like preschool graduation photos for your child

May 21, 2026
Instructions on how to change iPhone fonts beautifully with Lara
Tips

Instructions on how to change iPhone fonts beautifully with Lara

May 20, 2026
Create images of guardian mascots using AI according to year of birth
Tips

Create images of guardian mascots using AI according to year of birth

May 20, 2026
Try the Shopee Profit Calculator now, avoid the risk of selling at a loss
Tips

Try the Shopee Profit Calculator now, avoid the risk of selling at a loss

May 19, 2026
Next Post
Lesson 262: Goal Seek in Excel

Lesson 262: Goal Seek in Excel

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Recent News

How to turn off Gemini Nano on Chrome to free up 4GB of memory

How to turn off Gemini Nano on Chrome to free up 4GB of memory

May 30, 2026
Instructions for receiving Lovable Pro 12 months for free

Instructions for receiving Lovable Pro 12 months for free

May 29, 2026
How to extract text from photos using Zalo is super simple

How to extract text from photos using Zalo is super simple

May 29, 2026
HONOR 600 in hand – High-end design, 200MP camera, not cheap price

HONOR 600 in hand – High-end design, 200MP camera, not cheap price

May 28, 2026
How to turn off Gemini Nano on Chrome to free up 4GB of memory

How to turn off Gemini Nano on Chrome to free up 4GB of memory

May 30, 2026
Instructions for receiving Lovable Pro 12 months for free

Instructions for receiving Lovable Pro 12 months for free

May 29, 2026
How to extract text from photos using Zalo is super simple

How to extract text from photos using Zalo is super simple

May 29, 2026
AnonyViet - English Version

AnonyViet

AnonyViet is a website share knowledge that you have never learned in school!

We are ready to welcome your comments, as well as your articles sent to AnonyViet.

Follow Us

Contact:

Email: anonyviet.com[@]gmail.com

Main Website: https://anonyviet.com

Recent News

How to turn off Gemini Nano on Chrome to free up 4GB of memory

How to turn off Gemini Nano on Chrome to free up 4GB of memory

May 30, 2026
Instructions for receiving Lovable Pro 12 months for free

Instructions for receiving Lovable Pro 12 months for free

May 29, 2026
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office

wpDiscuz
0
0
Would love your thoughts, please comment.x
()
x
| Reply