How to build your first Python package?

Measure Space
3 min readMay 19, 2019

--

1. Use Github to create a project repository

Remember to choose .gitignore and license file. The .gitignore file can help you avoid submitting some intermediate files to git repository.

2. Download git repository

Find the git address of your repository, and download the code to your local directory by doing the following.

git clone https://github.com/xxxx/mydates.git

3. Package structure

Now it’s time to desgin your package structure. We are going to write a simple package called mydates with two modules - Dates and core. Dates module has one class Important_Dates(). This class is initialized with two variables: birth_day and wedding_day, and then use two functions get_days_passed_in_my_life() and get_days_passed_in_marriage() to calcualte the number of days passed. The core module has one test function print_test() which just print a test sentence.

We put the two modules under folder mydates. Here mydates is the name of our package. Under mydates folder we create an initial file __initi__.py to tell Python automatically include all the module files. The content of the __init__.py file is as follow:

from .core import *
from . import Dates

We show two different ways of including modules above. The statement from .core import * indicates that you can directly import functions from our package mydates such as from mydates import print_test. The statement from . import Dates means that we need to import Dates modules first such as from mydates import Dates in order to use classes or functions in Dates module.

Afte we have modules ready, we create another folder scripts for our script get_my_date.py. It's a test script as follow:

from mydates import Dates
from mydates import print_test
birth_day = '1999-09-09'
wedding_day = '2012-05-09'
mydays = Dates.Important_Dates(birth_day, wedding_day)mydays.get_days_passed_in_my_life()mydays.get_days_passed_in_marriage()print_test()

The whole structure of the package is as follow.

Package structure

4. Write setup.py file

To install our package mydates, we also need to write a setup.py file as follow:

from distutils.core import setupsetup(
name='mydates',
version='0.0.1',
author='X.X',
author_email='xx@example.com',
url='www.example.com',
license='LICENSE',
packages=['mydates'],
description='An example of building Python package.',
install_requires=[
#'python>=3.6.0',
#'pandas>=0.10.0'
]
)

Most time we need to install some required packages that are used in our own package. There are two ways to do so.

  • a) Write them under install_requires dictionary in setup.py
  • b) Write your own requirements.txt

The second method is a preferred method as it gives the most comprehensive list of all required packages. The detailed difference of these two methods can be found at here. One example of the requirements.txt is as follow:

python>=3.6.0
pandas>=0.1.0

5. Two ways of installing your package

Now we can install our first Python package mydates. Again, two ways we can use to install.

  • a) Official mode: pip install . under package root folder. This is similar as installing other Python packages.
  • b) Development mode: pip install -e . In this way, any changes we made can be reflected to the package immediately, and this is the typical way of installing a development model package.

Once we install the package, we can go to scripts folder and run the test script get_my_dates.py, it gives us the following results.

You have been living in this world for 7192 days!
You have been married for 2566 days!
This is a test.

Congrats! You finished your first Python package!

More tutorials and articles can be found at my blog-Measure Space and my YouTube channel.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response