Python is very convenient for us to develop a package. How to install your own python package and import it like numpy or other packages? Here, we’ll give a very simple example to you.

1 Prepare your python package

Create a empty directory called whatever, and here we name it Lib_Hello. Now give a name to your python package and we call it HelloPackage, and in directory HelloPackage, implement a python module. Here, we write a module named print_hello.py and crate a python file __init__.py in the same directory. You can write nothing in __init__.py. A file setup.py should be created as below,

1
2
3
4
5
6
from distutils.core import  setup

setup(name     = 'HelloPackage',
      version  = '1.0',
      author   = 'Geophydog',
      packages =  ['HelloPackage'])

name: the compressed package name;
version: the version no. of your python package;
author: of course you’re the author of this python package and just set it as your name;
packages: the the python package names, and write down the other package names in the list if you have other packages. Implement your module in file print_hello.py and the simple codes are given by the following lines,

1
2
3
4
5
6
class Hello:
        def __init__(self, hello):
                self.msg = hello
        def print(self, times):
                for i in range(times):
                        print(self.msg)

This module is a simple python class that print the message you give for several times. Lastly, the directory tree looks like

1
2
3
4
├── HelloPackage
│   ├── __init__.py
│   └── print_hello.py
└── setup.py

2 Build your package

In the directory Lib_Hello, just type command python setup.py build and a directory build will be generated. The directory tree is like

1
2
3
4
5
6
7
8
9
├── HelloPackage
│   ├── __init__.py
│   └── print_hello.py
├── build
│   └── lib
│       └── HelloPackage
│           ├── __init__.py
│           └── print_hello.py
└── setup.py

3 Distribute your package

Type python setup.py sdist in directory Lib_Hello and you’ll find a new directory dist. The directory tree is given by

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
├── HelloPackage
│   ├── __init__.py
│   └── print_hello.py
├── MANIFEST
├── build
│   └── lib
│       └── HelloPackage
│           ├── __init__.py
│           └── print_hello.py
├── dist
│   └── HelloPackage-1.0.tar.gz
└── setup.py

4 Install your package

Got into the directory dist and you’ll find a compressed package HelloPackage-1.0.tar.gz. Decompress the compressed package and go into the directory HelloPackage-1.0, and type python setup.py install to install your package.

5 Test your package

1
2
3
4
from HelloPackage.print_hello import Hello

h = Hello('Hello from Geophydog')
h.print(10)

The output will show

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Hello from Geophydog
Hello from Geophydog
Hello from Geophydog
Hello from Geophydog
Hello from Geophydog
Hello from Geophydog
Hello from Geophydog
Hello from Geophydog
Hello from Geophydog
Hello from Geophydog

So far, we’ve distributed and installed a python package. It’s very simple, isn’t it?