Build your own Python package (1)
Here is the content for the series of Build your own Python package:
- Make your package installable
- Generate doc for your package
- Format your codes
- Add testing for your codes
Make your package installable
Imagine that you have some python codes for analysis and many repetitive tasks. You might want to wrap up the codes, put them somewhere and just call them when you need them, like what you do with numpy
by import numpy as np
. In fact, you can achieve this with the help of Setuptools
, as introduced in the following.
“
Setuptools
is a collection of enhancements to the Python distutils that allow developers to more easily build and distribute Python packages, especially ones that have dependencies on other packages.” – cite from Setuptools website
Assume we have a package for installation named demo
.
- Install
setuptools
pip install setuptools
-
Prepare a
setup.py
file in the root dir of your packagecd demo vi setup.py
Here a simple example for
setup.py
:""" Setup script for `demo` """ from setuptools import setup, find_packages setup( name = "demo", version = "1.0", packages = find_packages(), include_package_data = True )
-
Install your package (copy the code to the path for python site-packages) by:
python setup.py install
If you will frequently modify your codes (e.g., when developing), I recommend you the development mode:
python setup.py develop
which will generate a link to your source code. Then you can change your source codes and apply the changes directly without re-installation.
-
If you want to specify the dependencies for your package, you can use
pipreqs
:pip install pipreqs pipreqs myproj
Enjoy Reading This Article?
Here are some more articles you might like to read next: