5 steps to build your first Python package
Nov 22, 2020 00:00 · 717 words · 4 minute read
Following 5 steps, you can build your own Python packages/libraries.
You may have used a lot of Python packages/libraries in your daily work, such as Pandas. Have you wondered how those libraries are built? By following a few steps, you can build your own package/library as well just like any major Python libraries. You can also share it on Github and let the open-source spirit shine. Because this is the way! Let’s get started!
1. Use Github to create a project repository
First, we need to create a repo on Github. Like the screenshot below, click the green “New” button to create a new repo on your Github.
Then we can give the package a name. Let’s say “mydates”. We can also add some description in the “Description” session. After that, we can set the repo to be “Public” and select “Add a README file”, which will make Github create a README markdown file when creating the new repo. Remember to choose .gitignore
and license file. The .gitignore
file can help us avoid subm
itting some intermediate development files to the git repository.
2. Download git repository
Find the git address of your repository, and download the code to your local directory by running the following command in your terminal. Of course, you need to replace “MeasureSpace” with your own Github name.
git clone https://github.com/MeasureSpace/mydates.git
3. Package structure
Now it’s time to desgin our package structure. We are going to write a simple package called mydates
with two modules - Dates
and core
.
Dates
module has one classImportant_Dates
. This class is initialized with two variables:birth_day
andwedding_day
, and then uses two functionsget_days_passed_in_my_life()
andget_days_passed_in_marriage()
to calculate the number of days passed.- The
core
module has one test functionprint_test()
which just prints 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 the 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.
4. Write setup.py file
To install the package mydates
, we need to write a setup.py
file as follow:
from distutils.core import setup
setup(
name='mydates',
version='0.0.1',
author='Measure Space',
author_email='xx@example.com',
url='www.example.com',
license='LICENSE',
packages=['mydates'],
description='An example of building a Python package.',
install_requires=[
'python>=3.0.0',
'pandas>=0.1.0'
]
)
Most time we need to install some required packages that are used in our own package. There are two ways to do so.
- Write them under
install_requires
dictionary insetup.py
- 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.0.0
pandas>=0.1.0
5. Two ways of installing your package
Now we can install our first Python package mydates
. There are also two ways we can use to install the package.
- Official mode: run
pip install .
in your terminal under the package root folder. This is similar as installing other Python packages. - Development mode: Also under the package root folder, run
pip install -e .
In this way, any changes we made can be reflected to the package immediately. This is the typical way of installing a development 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’ve developed your first Python package!