• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
LLODO – Education and technology

LLODO - Education and technology

Find your international education - university and college study education programs, student Exam, and course information.

  • Technology News
  • Blog Anony
  • Technology Quiz

How to package Python code

04/25/2022 by admin Leave a Comment

In this article, I will show you how to package Python code. You spent weeks perfecting your code. You have tested it and sent it to testers 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.

How to package your 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.

The 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 uppercase.

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 pack 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.

Code packaging

To create your Python package, you need a builder. One popular tool is i, which you can install 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.

Filed Under: Blog Anony Tagged With: Anony Hack, News Tech Anony

Related posts:

  1. 5 “Shadow” Chrome Extensions You Need To Delete Immediately
  2. 5 Websites to help you find out who the Email account holder is
  3. How to protect website copyright with DMCA
  4. What is CTF? If you want to be a Hacker, should you play CTF?
  5. How to hide your home from Google Maps
  6. Phishing Site Creation Tool for Linux and Android
  7. Use AI to turn sketches into real photos
  8. Tips to find other people’s information on the Internet
  9. Calendar’s File .ics phishing attack technique
  10. How to fix “This Site Can’t Be Reached” error ERR_ADDRESS_UNREACHABLE
  11. How to Get Free Remitano’s RENEC Tokens
  12. 14 Google Sheets functions that Excel doesn’t have
  13. Experience in Website Security Testing
  14. How to download all Instagram users’ photos and videos
  15. Change Header to Bypass WAF when scanning Website vulnerabilities with Scan Tools
  16. List of 15 largest hacked companies in history
  17. Use Netcat to Transfer Files in Windows and Linux
  18. Top 3 Safe and Clean Game Crack Download Websites
  19. TOP 5 Wifi Routers under 500,000 VND suitable for families in 2022
  20. How to secretly Track your phone using xfi endpoint

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • 5 “Shadow” Chrome Extensions You Need To Delete Immediately
  • There may be a parallel reality existing with us, right here and now
  • Fearing catastrophic climate change, scientists want to store all mankind’s knowledge on the Moon’s volcano
  • As a man in the house, it is indispensable for screwdrivers, electric drills, to repair anything is as easy as turning your hand
  • Summer is here, install solar panels! But, read some of these tips first




Categories

  • Blog Anony (387)
  • Family and Friends 1 (63)
  • Family and Friends 2 (80)
  • Family and Friends 3 (80)
  • Family and Friends 4 (84)
  • Family and Friends 5 (82)
  • Grade 1 Math (61)
  • Grade 2 Math (96)
  • Grade 3 English (68)
  • Grade 3 Math (67)
  • Grade 4 English (68)
  • Grade 4 Math (77)
  • Grade 5 English (68)
  • Grade 5 Math (88)
  • Grade 6 English (104)
  • Grade 6 Math (67)
  • Grade 6 Physics (30)
  • Grade 7 English (104)
  • Grade 7 Math (57)
  • Grade 7 Physics (30)
  • Grade 8 Biology (64)
  • Grade 8 Chemistry (43)
  • Grade 8 English (104)
  • Grade 8 Math (75)
  • Grade 8 Physics (29)
  • Grade 9 Biology (63)
  • Grade 9 Chemistry (56)
  • Grade 9 English (104)
  • Grade 9 Math (61)
  • Grade 9 Physics (62)
  • Houseware (126)
  • Learning English (50)
  • Linux Quiz (300)
  • Software (31)
  • Technology News (7,495)
  • Technology Quiz (850)
  • Windows Quiz (200)

Copyright (c) 2022 · LLODO.COM - About Us - Privacy Policy - Contact Us - Site map
Link: Question Answer English - Hoc edu - Internet Do