Table of Contents
- Introduction
- Prerequisites
- Setup
- Creating a Python Package
- Adding Functionality
- Testing
- Publishing on PyPI
- Conclusion
Introduction
In this tutorial, we will learn how to create a Python package from scratch and publish it on PyPI (Python Package Index). By the end of this tutorial, you will have a clear understanding of the process involved in creating a package, adding functionality to it, testing it, and finally publishing it on PyPI for others to use.
Prerequisites
Before you start this tutorial, you should have a basic understanding of Python programming language and familiarity with command line tools.
Setup
To get started, make sure you have Python installed on your system. You can download and install the latest version of Python from the official Python website (https://www.python.org/downloads/).
Additionally, make sure you have pip installed. Pip is the package installer for Python that will help us install and manage packages. You can check if pip is installed by running the following command in your terminal:
pip --version
If pip is not installed, you can install it by following the instructions on the official pip website (https://pip.pypa.io/en/stable/installing/).
Once you have Python and pip set up, we can proceed to create our Python package.
Creating a Python Package
-
Choose a name for your package. It should be unique and descriptive. For this tutorial, we will use the name “my_package” as an example.
-
Create a new directory on your computer where you want to store your package. You can choose any location that is convenient for you. Open a terminal or command prompt and navigate to the directory you just created.
- Inside the directory, create a new directory with the same name as your package. In this case, run the following command:
mkdir my_package
- Navigate into the newly created directory:
cd my_package
- Initialize the package by running the following command:
pip init
This command will prompt you to enter various details about your package, such as name, version, description, author, etc. You can provide the required information based on your package’s specifications.
-
After providing the information, a
setup.py
file will be generated in your package directory. This file contains the configuration settings for your package. You may need to modify thesetup.py
file later to add more advanced settings depending on your package requirements. - At this point, your package structure should look like this:
my_package/ setup.py
Congratulations! You have successfully created a Python package.
Adding Functionality
Now that we have our package set up, let’s add some functionality to it. There are multiple ways to organize your package’s code, but we will follow a simple structure for this tutorial.
- Inside the
my_package
directory, create a new directory calledsrc
. This directory will contain your Python modules.mkdir src
- Navigate into the
src
directory:cd src
- Create a new Python file called
my_module.py
:touch my_module.py
- Open
my_module.py
in a text editor and add the following code:def greet(name): """ A function that greets the user by their name. """ return f"Hello, {name}!"
In this example, we have a simple function
greet
that takes a name as an argument and returns a personalized greeting. - Save the file and navigate back to the root of your package:
cd ..
- Open the
setup.py
file in a text editor and add the following line under thesetup()
function:packages=['my_package'],
This line tells Python to include the
my_package
directory when packaging your module. - Your
setup.py
file should now look like this:from setuptools import setup setup( name='my_package', version='0.1', packages=['my_package'], url='https://github.com/your_username/my_package', license='MIT', author='Your Name', author_email='[email protected]', description='A short description of your package', )
Save the
setup.py
file.
Congratulations! You have added functionality to your Python package.
Testing
Before we publish our package on PyPI, it’s essential to test it to ensure everything is working as expected.
-
Open a terminal or command prompt and navigate to the root of your package.
- Run the following command to install your package in editable mode:
pip install -e .
This command installs your package and its dependencies, but in a way that allows you to make changes to the code without reinstalling.
-
Now, open a Python shell by typing
python
in your terminal. - Import your package and test the
greet
function:from my_package.my_module import greet print(greet('John'))
You should see the output
Hello, John!
.
If the output is as expected, it means your package is working correctly.
Publishing on PyPI
Now that we have tested our package, we are ready to publish it on PyPI. However, before you can publish your package, you need to create an account on the PyPI website (https://pypi.org/).
-
Once you have created an account, open a terminal or command prompt and navigate to the root of your package.
- Run the following command to build the package distribution files:
python setup.py sdist bdist_wheel
This command creates two types of distribution files: a source distribution (
sdist
) and a binary distribution (bdist_wheel
), which is platform-specific. - Install the
twine
package, which is used to upload packages to PyPI. Run the following command:pip install twine
- Once
twine
is installed, run the following command to upload your package:twine upload dist/*
This command will prompt you to enter your PyPI username and password. Once you provide the credentials,
twine
will upload your package to PyPI.
Congratulations! You have successfully published your Python package on PyPI.
Conclusion
In this tutorial, we learned how to create a Python package from scratch, add functionality to it, test it, and publish it on PyPI. By following these steps, you can share your code with the Python community and make it easily accessible to others.
Remember to keep improving your package, maintaining it, and responding to any user feedback or bug reports to ensure its success in the long run. Happy packaging!