Python Package: Learn to Create Your Own

DZone 's Guide to

Python Package: Learn to Create Your Own

In this article, we provide you with a quick and easy tutorial on how to create Python packages to better modularize your code.

· Web Dev Zone ·
Free Resource

Packages provide one of the best ways to structure multiple modules and small packages. It can help users to set the data hierarchy in a well-organized hierarchy manner and makes it quite easy for them to create and access any of the directories and modules as per their requirement. 

It is just similar to folders and drivers available in our operating system, which help us to store multiple files, media, and much more. Similarly, if users learn to build packages, then it can help them to store other modules and sub-packages. Users can use and can modify any of the packages, as per their project needs whenever they want.

Definition of a Python Package

The Python Package Index is abbreviated as PyPI and is well known as the Cheese Shop. It is official third-party software that is represented for Python developers or users and is analogous to CPAN, which is the repository for Perl. Some package managers such as pip make use of PyPI by considering it as the default source for created packages and also for their dependencies. (Source).

In short, packages can refer to as namespaces that contain the end number of modules and packages within themselves. It is simply a directory with little twists and turns. Each Python package is a directory which MUST include a very special file known as __init__.py. This file can be kept empty while creating it, later it indicates that the directory which contains all the Python package which users create. Users can import any of the packages in the same manner in which they import modules for their python project. 

For Example

Creating and listing the directory with python becomes much easier. If you want, then you can create a directory and name it as "foo", which simply marks the package name. Later, you can create a module inside the package and name it, "bar". But, make sure that you don't forget to add the __init__.py file. Add it inside the directory, which you have created and named it as "foo". 

Developing and Accessing a Package in Python 

Creating a Python package can sound to be a bit overwhelming; the primary reason for this is confusion, regarding the right way of creating and using Python packages. But, users need to understand that it's not hard to create a package in Python — even after their first package creation, users will just feel very easy. Even if they learn the right method for creating a Python package, it becomes quite easy for them to create a Python package, as per their requirements. 

Users must learn to create packages even though they are not interested in distributing their code as open-source as it can help them to prepare themselves for the future. By gaining knowledge about Python package creation, they can easily have insight into the whole package ecosystem. 

Simples Steps to Follow to Create Python Package

Create a package name "MyPackage" by following the below-listed steps:

  • Create a folder and give it a name of your choice. 
  • Inside that folder, create a subfolder, and name it "package".
  • Now, create an empty "__init__.py" file in that "packageæ folder.
  • Use a Python-aware editor, such as IDLE to create multiple of your choice in the created package.

Simple Example of Creating a Python Package

To give you proper knowledge about the Python package, let's consider a small example with the code. 

Step 1. First, create a directory, and give it the name "Cars".

Step 2. Now, create modules. To do so, create a file named Ford.py. To remember for what you have developed, the code creates content using #.  

code: ford.py


Now, let's create another file and name it AMC Gremlin.py and create code for it by using different members to distinguish it from the first one.

Code: AMC Gremlin.py


Now, let's create another file by giving it a name McLaren Senna.py. Add simple code to the file and different members to differentiate it from the above two.

Code: McLaren Senna.py


Step 3: Now, let's create the __init__.py file. Make sure to place this file inside the Cars directory, which has been created previously. This file can be left blank or create initialization code as follow:

 Code: __init__.py file 
from Ford import Ford  
from AMCGremlin import AMCGremlin
from McLarenSenna import McLarenSenna 

Now, let's explore the package we have created. To so, create a sample.py file, make sure to create it in the same directory where you have created Cars package, and add the listed below code: 


Code: (Source)

Users can access the developed package in numerous ways as per their requirements. They can even change the needed information whenever they need by making some changes in the developed code that they have written while creating a package of their choice. 

Ways to Split Large Application Code into Small Packages

One of the most accurate ways of writing code for an application is to split it into different and small packages. This makes it easier for developers to understand, change, and maintain code for big applications much easier. It also enables them to reuse the maximum amount of code that they have previously written previously. 

The root directory contains a setup.py script. All the metadata, which is described in the distutils module, can easily be defined using it. Package metadata can be expressed as arguments when it is called to the standard setup() function; distutils can be considered as a standard library module that is provided to users for writing code while creating a package. Developers or users can also recommend using setup tools instead of standard python libraries. The package provides numerous enhancements to the users over distutils module.

Minimum code for setup.py file can be written as follow:

Code: Setup.py


The name provides the whole name of the package. It allows the script to provide several commands, which are listed with the help of the help command option. This will help you explore the following code.

 $ python3 setup.py  


Standard commands:

build  everything needed to install
clean clean up temporary files from 'build' command
install everything from the build directory
sdist create a source distribution (tarball, zip file, etc.)
register the distribution with Python package index
bdist create a built (binary) distribution
check to perform some checks on the
upload a binary package to PyPI

Extra commands:

bdist_wheel create a wheel distribution
alias define a shortcut to invoke one or more commands
develop install package in 'development mode'

 usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] 

 or: setup.py --help [cmd1 cmd2 ...] 

 or: setup.py --help-commands 

 or: setup.py cmd --help 

Code: (Source)

The actual list of help commands is much longer. It depends on the setuptools extensions, which developers or users use while developing Python packages.


When it comes to creating a distribution with the sdist command, then the distutils module searches for the package directory to find those files to include in the archive. Some of the default distutils will make the use of the following:

  • Most of the Python source files which are implied by the py_modules, scripts arguments, and packages. 

  • C source files which are listed in the ext_modules argument. 

  • FIles that matches with the glob pattern such as test/test*.py

  • Files named README.txt, README, setup.cfg, setup.py, and many others.   

Additionally, if your created package poses a version control system such as Git, Mercurial, Subversion, then there is a great possibility that it will include all version-controlled files. This file might include additional files like setuptools-hg, setuptools-svn, setuptools-git, and many others. But by considering sdist for creating a MANIFEST file, developers or users can list all files they want to and can also include the same in the final archive. All the MANIFEST.in the template can be defined all the rules for inclusion or exclusion per line:



Code: (Source)

List of Most Important MetaData Which You Can Consider for Python Package

The version and name of the Python package are distributed at the initial level that is when it is being installed. But besides that, it has many important arguments which are received by the setup() functions that are describe as follows: 

  • description: It helps to describe the package with a few sentences. 

  • long_description: The whole description is included in it that is all about the supported mark-up languages or reStructuredText.

  • keywords: This includes the list of keywords that are being used to define a package.  

  • url: Describes the URL of the created project.

  • packages: This includes the list of all packages according to its name and distribution. 

There are many other functions, like long_description_content_type, author, author_email, license, and many more, which provides users much more advancement when they think of developing python packages for their first project. It becomes quite easy for them to craft any size of the project with the help of a python package.

It's Time to Create Your Own Python Package!

Python not only smoother development process but also helps developers to save a considerable amount of time as they can use the same code multiple times while developing their dream project. By having little knowledge about python packages, they can create numerous packages for their project and call it whenever required. 

Hopefully, now you might have understood how you can gain access to functionality, such as the built-in and third-party modules available in Python. If you are a developer and aim to modularize our app or software development code, then it becomes quite easy for you when you consider developing the python package. It even allows you to develop the needed modules which helps you make the coding cycle much easier and faster, it also allows you to debug your project during the development phase, this ensures you to come up with advance and bug-free projects. 

python ,python 101 ,python packages ,tutorial ,web dev

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}