How to Distribute and Install Your Own Python Package
Contents
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,
|
|
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,
|
|
This module is a simple python class that print the message you give for several times. Lastly, the directory tree looks like
|
|
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
|
|
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
|
|
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
|
|
The output will show
|
|
So far, we’ve distributed and installed a python package. It’s very simple, isn’t it?
Author Geophydog
LastMod 2020-09-22