• 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

Windows can now run Linux applications
Tips

Windows can now run Linux applications

March 17, 2026
New features of Windows 10 21H1 are coming soon
Tips

New features of Windows 10 21H1 are coming soon

March 16, 2026
How to convert normal music into 8D music with Audioalter
Tips

How to convert normal music into 8D music with Audioalter

March 14, 2026
How to prevent your family’s Security Camera from being hacked
Tips

How to prevent your family’s Security Camera from being hacked

March 13, 2026
Pixel Agents: Turn VS Code into a Pixel Art Office for AI
Tips

Pixel Agents: Turn VS Code into a Pixel Art Office for AI

March 12, 2026
English learning app Memrise is offering a lifetime discount of only 21,000 VND
Tips

English learning app Memrise is offering a lifetime discount of only 21,000 VND

March 11, 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

Windows can now run Linux applications

Windows can now run Linux applications

March 17, 2026
How to install extension to remind office workers to drink water

How to install extension to remind office workers to drink water

March 17, 2026
Why are iPhones and MacBooks still the number 1 “weapons” of the technology world?

Why are iPhones and MacBooks still the number 1 “weapons” of the technology world?

March 16, 2026
New features of Windows 10 21H1 are coming soon

New features of Windows 10 21H1 are coming soon

March 16, 2026
Windows can now run Linux applications

Windows can now run Linux applications

March 17, 2026
How to install extension to remind office workers to drink water

How to install extension to remind office workers to drink water

March 17, 2026
Why are iPhones and MacBooks still the number 1 “weapons” of the technology world?

Why are iPhones and MacBooks still the number 1 “weapons” of the technology world?

March 16, 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

Windows can now run Linux applications

Windows can now run Linux applications

March 17, 2026
How to install extension to remind office workers to drink water

How to install extension to remind office workers to drink water

March 17, 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