Build a Python Package with setuptools

imflorachen
2 min readOct 29, 2020

--

1. Create a virtual environment

  • using pipenv
# build vertual envpipenv --python 3.6  # pipenv --three 
# activate
pipenv shell
# install pkg
pipenv install requests # for default env
pipenv install flake8 --dev # for development env

2. Create function package

# the folder structure
setup-demo/
├ setup.py
├ MANIFEST.in
└ myapp/
├ __init__.py
├ function/ # my function code
├ __init__.py
├ myfunction.py
├ static/ # static folder
├ __init__.py
├ myfile.csv
...

To change the import path

  • a dot “.” means go up to that directory’s parent directory
  • if import static file in the python scripts, should use importlib.resources to read the file
# myfunction.py 
from .. import static

try:
import importlib.resources as pkg_resources
except ImportError:
# Try backported to PY<37 `importlib_resources`.
import importlib_resources as pkg_resources
with pkg_resources.path(util, "myfile.csv") as wd:
my_table = pd.read_csv(wd)
  • besides using importlib.resources, should also create a “MANIFEST.in” and set include_package_data=True in “setup.py
# MANIFEST.in 
include myapp/static/myfile.csv
  • __init__.py
  1. create an empty __init__.py in each folder
  2. the __init__.py in setup-demo folder should import all class
# __init__.py in setup-demo 
from .myapp.myfunction import MyClass1, MyClass2, MyClass3

3. Create setup.py

import setuptools  
setuptools.setup(
name="mypackage", # Replace with your own username
version="0.1.0",
author="me",
author_email="me@example.com",
description="write descr here",
url="<https://github.com/py/sampleproject>",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
install_requires=[
"scipy>=1.5.2",
"numpy>=1.19.2",
"matplotlib>=3.3.2",
"pandas>=1.1.2"
],
python_requires=">=3.5, <3.8",
include_package_data=True,
exclude_package_date={"": [".gitignore", ".gitkeep"]}
)

4. Build up

  • Change the directory to setup-demo, and use the command below to build up package. It will generate two folders after build up, the demo.egg-info and dist, the package is in dist
# develope mode 
python setup.py develop
python setup.py develop --uninstall
# build .tar.gz
python setup.py sdist
# build .zip
python setup.py bdist
# build .whl
python setup.py bdist_wheel

5. Testing

  • test in develop mode: after build up it won’t generate dist, you can change directory to setup-demo, type python and try import mypackage
  • test .tar.gz (or .whl) in another environment:
# build a new folder and put package in the folder 
pipenv --python 3.7
pipenv shell
pipenv install mypackage.tar.gz
# (or xxxx.whl which is generated in dist)
pipenv install pytest -dev
pytest testscript.py

reference: http://www.bjhee.com/setuptools.html

--

--

imflorachen
imflorachen

Written by imflorachen

Hi i’m trying to write what i’ve learned from work or from books. Wish I could become a better data scientist.

No responses yet