home..

Pypi

Creating Python packages with PyPI

Packaging Python code for distribution on the Python Package Index (PyPI) involves several steps. Here is a step-by-step tutorial on how to package your Python code for PyPI.

Getting Started

Prerequisites

Before you start, make sure you have the following:

Step 1: Create a project directory

Create a new directory for your project, and navigate into it:

$ mkdir myproject
$ cd myproject

Step 2: Create your package directory

Create a new directory inside your project directory, conventionally with the same name as your project directory (myproject in this example). This directory should contain your package code.

Your project directory should look like this:

myproject/
    | myproject/
    | ...

Step 3: Create a setup.py file

Create a new file called setup.py in your project directory. This is the key file for packaging your codes.

This file will contain information about your package, including its name, version, and dependencies. Here is an example setup.py file:

from setuptools import setup

setup(
    name='myproject',
    version='0.1.0',
    python_requires='>=3.8',
    description='My project description',
    author='Your Name',
    author_email='your.email@example.com',
    packages=['myproject'],
    install_requires=[
        'numpy>=1.19.0',
    ],
)

In this example, we specify the name of the package (myproject), its version (0.1.0), a brief description, the author’s name and email address, the list of folders that should be packaged (in this case, just myproject/), and the dependencies required to run the package (in this example, numpy version 1.19 or above).

Now, your project directory should look like this:

myproject/
    | myproject/
    | setup.py
    | ...

Step 4: Package your project

The following is the key command to package your project:

$ python setup.py sdist bdist_wheel

This will create a few new directories in your current directory. In particular, it will create a dist directory which contains the stuff that will get uploaded.

Question: What other directories were created?

Step 5: Upload your package to TestPyPI

To upload your package to PyPI, you need to have a PyPI account. Once you have an account, you can upload your package using the twine package. To install twine, run the following command:

$ pip install twine

Once twine is installed, you’re ready to upload your package. Before you upload your package to PyPI, it is good practice to first upload it to TestPyPI: This is a clone provided by PyPI for users to upload and test their packages:

$ twine upload --repository-url https://test.pypi.org/legacy/ dist/* 

This will upload your package to TestPyPI.

Question: Was the upload successful? Why? Why not?

Question: Once your upload succeeded, what happens if you run the command again immediately after?

To test your published package, open a new tab and run the following command:

$ pip install --extra-index-url https://test.pypi.org/simple/ myproject

Step 6: Upload your package to PyPI

Finally, if your previous test was successful, you can upload to PyPI with the following command:

$ twine upload dist/*

Users should now be able to install your package with the following command:

$ pip install myproject

Congratulations! Your package is now available on PyPI for others to install and use.

© 2023 Oxford Wearables Group