Creating Python Packages

This site will inform you how to create your own Python Packages on Pypi.org . This is a global public site that is accessible to anyone around the world. It allows anyone to pip install <yourpackage>

Pre-requisite

You must perform the following pre-requistes.

Important

  1. Create an account on Pypi.org

  2. You must also generate a token. This token should start with pypi-. SAVE THIS TOKEN. You will need it to upload your package to Pypi.org

  3. Install the following packages:

  1. Python 3.12 or greater

  2. pip install twine==6.2.0
    
  1. pip install setuptools==80.9.0
    
  1. Create local folder on your machine. You can choose proper name. For our example, we will create a folder called: pythonpackage

    1. Inside pythonpackage create your package. For our example, we created studenttestpackage

    2. Inside studenttestpackage create another folder with the SAME name: studenttestpackage

    _images/tp1.png

    Important

    You MUST choose your own unique python package name. You CAN NOT choose: studenttestpackage it is already an existing python package under a different username.

How to Create Your Own Python Packages

Tip

All files for this demo are located on Github

Follow these steps

  1. Goto folder: c:/>pythonpackage/studenttestpackage

  2. Download the Github files locally to your computer from here: studenttestpackage

    _images/tp2.png
  3. Goto folder: c:/>pythonpackage/studenttestpackage/studenttestpackage

  4. Download the Github files locally to your computer from here: studenttestpackage/studenttestpackage

    _images/tp3.png
  5. In the file c:/>pythonpackage/studenttestpackage/setup.py

    You MUST have the following:

    1. name=’studenttestpackage’, (THIS MUST MATCH THE NAME OF YOUR PACKAGE)

    2. packages=[‘studenttestpackage’], (THIS MUST MATCH THE NAME OF YOUR PACKAGE)

    _images/tp4.png
  6. Goto folder: c:/>pythonpackage/studenttestpackage/studenttestpackage

    _images/tp5.png
  7. In the file c:/>pythonpackage/studenttestpackage/studenttestpackage/__init__.py

    This is the file that EXPORTS your python function that you write. In this example all functions are defined in file myfunctions.py

You MUST have the following:

  1. name = “studenttestpackage” (THIS MUST MATCH THE NAME OF YOUR PACKAGE)

_images/tp7.png
  1. The function you define are exported in lines:

    1. from .myfunctions import sayhello

    2. from .myfunctions import saygoodbye

  1. In the file c:/>pythonpackage/studenttestpackage/studenttestpackage/myfunctions.py

    This is the file where you define your functions.

_images/tp8.png
  1. The other files in c:/>pythonpackage/studenttestpackage/

    1. requirements.txt (This is where you can define additional packages you need and Python will automatically download them so users of your package DO NOT have to)

    2. license.txt (This is the file you can define any licensing for your package)

    3. readme.md (This is the file where you write the documentation for your package. For example, how to use the functions in myfunctions.py

Uploading Your Package to Pypi

You are ready to upload your package. Follow these steps:

  1. Goto folder: c:/>pythonpackage/studenttestpackage/

    1. Execute the command 1:

      python setup.py sdist bdist_wheel
      
    _images/tp9.png
    1. Execute the command 2:

      twine upload dist/*
      
    _images/tp10.png
    1. Enter Your Token:

      _images/tp12.png
  2. See your package on Pypi.org: studenttestpackage

    _images/tp11.png
  3. Install your package

    pip install studenttestpackage==1.0
    
    _images/tp13.png
  4. Run your Python function in your Package: testprogram.py

    _images/tp14.png
  5. SUCCESSFUL RESULT!

    _images/tp15.png

Modify Files to Build Your OWN Python Package

The above files will produce a python package studenttestpackage - but you want to create a NEW python package for yourself. To do this you can easily modify the folders and files.

Important

Everytime you make changes to your package you MUST provide a new version number in the setup.py

_images/tp16.png
  1. You want to create OWN Package. To the following:

  1. Modify the name of the package: studenttestpackage to your desired name i.e. bugsbunny in steps 4.a and 4.b

  2. Modify 9.a and 9.b to your new package name bugsbunny

    1. name=’bugsbunny’, (THIS MUST MATCH THE NAME OF YOUR PACKAGE)

    2. packages=[‘bugsbunny’], (THIS MUST MATCH THE NAME OF YOUR PACKAGE)

  3. Modify 11.a

    1. name = “bugsbunny” (THIS MUST MATCH THE NAME OF YOUR PACKAGE)

  4. Re-Run: 14.a, 14.b, 14.c

  1. Install your package

    pip install bugsbunny==1.0
    
  2. import the package in your Python program

    import bugsbunny as tp