Installing your own
package
D E V E L O P I N G P Y T H O N PA C K A G E S
James Fulton
Climate informatics researcher
Why should you install your own package?
Inside example_script.py Directory tree for package with subpackages
import mysklearn home/
|-- mysklearn <-- in same directory
| |-- __init__.py
| |-- preprocessing
| | |-- __init__.py
| | |-- [Link]
| | |-- [Link]
| |-- regression
| | |-- __init__.py
| | |-- [Link]
| `-- [Link]
|-- example_script.py <-- in same directory
DEVELOPING PYTHON PACKAGES
Why should you install your own package?
Inside example_script.py Directory tree
import mysklearn home/
|-- mypackages
| |-- mysklearn <---
Traceback (most recent call last):
| |-- __init__.py
File "<stdin>", line 1, in <module>
| |-- preprocessing
ModuleNotFoundError: No module named 'mysklearn'
| | |-- __init__.py
| | |-- [Link]
| | |-- [Link]
| |-- regression
| |-- __init__.py
| |-- [Link]
`-- myscripts
`-- example_script.py <---
DEVELOPING PYTHON PACKAGES
[Link]
Is used to install the package
Contains metadata on the package
DEVELOPING PYTHON PACKAGES
Package directory structure
Directory tree for package with subpackages
mysklearn/
|-- __init__.py
|-- preprocessing
| |-- __init__.py
| |-- [Link]
| |-- [Link]
|-- regression
| |-- __init__.py
| |-- [Link]
|-- [Link]
DEVELOPING PYTHON PACKAGES
Package directory structure
Directory tree for package with subpackages
mysklearn/ <-- outer directory
|-- mysklearn <--- inner source code directory
|-- __init__.py
|-- preprocessing
| |-- __init__.py
| |-- [Link]
| |-- [Link]
|-- regression
| |-- __init__.py
| |-- [Link]
|-- [Link]
DEVELOPING PYTHON PACKAGES
Package directory structure
Directory tree for package with subpackages
mysklearn/ <-- outer directory
|-- mysklearn <--- inner source code directory
| |-- __init__.py
| |-- preprocessing
| | |-- __init__.py
| | |-- [Link]
| | |-- [Link]
| |-- regression
| | |-- __init__.py
| | |-- [Link]
| |-- [Link]
|-- [Link] <-- setup script in outer
DEVELOPING PYTHON PACKAGES
Inside [Link]
# Import required functions
from setuptools import setup
# Call setup function
setup(
author="James Fulton",
description="A complete package for linear regression.",
name="mysklearn",
version="0.1.0",
)
version number = (major number) . (minor number) . (patch number)
DEVELOPING PYTHON PACKAGES
Inside [Link]
# Import required functions
from setuptools import setup, find_packages
# Call setup function
setup(
author="James Fulton",
description="A complete package for linear regression.",
name="mysklearn",
version="0.1.0",
packages=find_packages(include=["mysklearn", "mysklearn.*"]),
)
DEVELOPING PYTHON PACKAGES
Editable installation
pip install -e . Directory tree for package with subpackages
. = package in current directory mysklearn/ <-- navigate to here
|-- mysklearn
-e = editable | |-- __init__.py
| |-- preprocessing
| | |-- __init__.py
| | |-- [Link]
| | |-- [Link]
| |-- regression
| | |-- __init__.py
| | |-- [Link]
| |-- [Link]
|-- [Link]
DEVELOPING PYTHON PACKAGES
Let's practice!
D E V E L O P I N G P Y T H O N PA C K A G E S
Dealing with
dependencies
D E V E L O P I N G P Y T H O N PA C K A G E S
James Fulton
Climate informatics researcher
What are dependencies?
Other packages you import inside your package
Inside [Link] :
# These imported packages are dependencies
import numpy as np
import pandas as pd
...
DEVELOPING PYTHON PACKAGES
Adding dependencies to [Link]
from setuptools import setup, find_packages
setup(
...
install_requires=['pandas', 'scipy', 'matplotlib'],
)
DEVELOPING PYTHON PACKAGES
Controlling dependency version
from setuptools import setup, find_packages
setup(
...
install_requires=[
'pandas>=1.0',
'scipy==1.1',
'matplotlib>=2.2.1,<3'
],
)
DEVELOPING PYTHON PACKAGES
Controlling dependency version
from setuptools import setup, find_packages
setup(
...
install_requires=[
'pandas>=1.0', # good
'scipy==1.1', # bad
'matplotlib>=2.2.1,<3' # good
],
)
Allow as many package versions as possible
Get rid of unused dependencies
DEVELOPING PYTHON PACKAGES
Python versions
from setuptools import setup, find_packages
setup(
...
python_requires='>=2.7, !=3.0.*, !=3.1.*',
)
DEVELOPING PYTHON PACKAGES
Choosing dependency and package versions
Check the package history or release notes
e.g. the NumPy release notes
Test different versions
DEVELOPING PYTHON PACKAGES
Making an environment for developers
pip freeze
alabaster==0.7.12
appdirs==1.4.4
argh==0.26.2
...
wrapt==1.11.2
yapf==0.29.0
zipp==3.1.0
DEVELOPING PYTHON PACKAGES
Making an environment for developers
Save package requirements to a file mysklearn/
|-- mysklearn
pip freeze > [Link] | |-- __init__.py
| |-- preprocessing
| | |-- __init__.py
Install requirements from file
| | |-- [Link]
| | |-- [Link]
pip install -r [Link]
| |-- regression
| | |-- __init__.py
| | |-- [Link]
| |-- [Link]
|-- [Link]
|-- [Link] <-- developer environment
DEVELOPING PYTHON PACKAGES
Let's practice!
D E V E L O P I N G P Y T H O N PA C K A G E S
Including licences
and writing
READMEs
D E V E L O P I N G P Y T H O N PA C K A G E S
James Fulton
Climate informatics researcher
Why do I need a license?
To give others permission to use your code
DEVELOPING PYTHON PACKAGES
Open source licenses
Find more information here
Allow users to
use your package
modify your package
distribute versions of your package
1 [Link]
DEVELOPING PYTHON PACKAGES
What is a README?
The "front page" of your package
Displayed on Github or PyPI
DEVELOPING PYTHON PACKAGES
What to include in a README
README sections
Title
Description and Features
Installation
Usage examples
Contributing
License
DEVELOPING PYTHON PACKAGES
README format
Markdown (commonmark) reStructuredText
Contained in [Link] file Contained in [Link] file
Simpler More complex
Used in this course and in the wild Also common in the wild
DEVELOPING PYTHON PACKAGES
Commonmark
Contents of [Link] What it looks like when rendered
DEVELOPING PYTHON PACKAGES
Commonmark
Contents of [Link] What it looks like when rendered
# mysklearn mysklearn
mysklearn is a package for complete
mysklearn is a package for complete linear
**linear regression** in Python.
regression in python.
You can find out more about this package
on [DataCamp]([Link] You can find out more about this package on
DataCamp
DEVELOPING PYTHON PACKAGES
Commonmark
Contents of [Link] What it looks like when rendered
# mysklearn mysklearn
mysklearn is a package for complete
mysklearn is a package for complete linear
**linear regression** in Python.
regression in python.
You can find out more about this package
on [DataCamp]([Link] You can find out more about this package on
DataCamp
## Installation
You can install this package using
Installation
You can install this package using
DEVELOPING PYTHON PACKAGES
Commonmark
Contents of [Link] What it looks like when rendered
# mysklearn mysklearn
mysklearn is a package for complete
mysklearn is a package for complete linear
**linear regression** in Python.
regression in python.
You can find out more about this package
on [DataCamp]([Link] You can find out more about this package on
DataCamp
## Installation
You can install this package using
Installation
``` You can install this package using
pip install mysklearn
``` pip install mysklearn
DEVELOPING PYTHON PACKAGES
Adding these files to your package
Directory tree for package with subpackages
mysklearn/
|-- mysklearn
| |-- __init__.py
| |-- preprocessing
| | |-- ...
| |-- regression
| | |-- ...
| |-- [Link]
|-- [Link]
|-- [Link]
|-- LICENSE <--- new files
|-- [Link] <--- added to top directory
DEVELOPING PYTHON PACKAGES
[Link]
Lists all the extra files to include in your package distribution.
DEVELOPING PYTHON PACKAGES
[Link]
Contents of [Link] mysklearn/
|-- mysklearn
include LICENSE | |-- __init__.py
include [Link] | |-- preprocessing
| | |-- ...
| |-- regression
| | |-- ...
| |-- [Link]
|-- [Link]
|-- [Link]
|-- LICENSE
|-- [Link]
|-- [Link] <---
DEVELOPING PYTHON PACKAGES
Let's practice!
D E V E L O P I N G P Y T H O N PA C K A G E S
Publishing your
package
D E V E L O P I N G P Y T H O N PA C K A G E S
James Fulton
Climate informatics researcher
PyPI
Python Package Index
pip installs packages from here
Anyone can upload packages
You should upload your package as soon as it might be useful
1 [Link]
DEVELOPING PYTHON PACKAGES
Distributions
Distribution package - a bundled version of your package which is ready to install.
Source distribution - a distribution package which is mostly your source code.
Wheel distribution - a distribution package which has been processed to make it faster to
install.
DEVELOPING PYTHON PACKAGES
How to build distributions
python [Link] sdist bdist_wheel mysklearn/
|-- mysklearn
sdist = source distribution |-- [Link]
bdist_wheel = wheel distribution |-- [Link]
|-- LICENSE
|-- [Link]
|-- dist <---
| |-- [Link]
| |-- [Link]
|-- build
|-- [Link]-info
DEVELOPING PYTHON PACKAGES
Getting your package out there
Upload your distributions to PyPI mysklearn/
|-- mysklearn
twine upload dist/* |-- [Link]
|-- [Link]
Upload your distributions to TestPyPI |-- LICENSE
|-- [Link]
twine upload -r testpypi dist/* |-- dist
| |-- [Link]
| |-- [Link]
|-- build
|-- [Link]-info
DEVELOPING PYTHON PACKAGES
How other people can install your package
Install package from PyPI
pip install mysklearn
Install package from TestPyPI
pip install --index-url [Link]
--extra-index-url [Link]
mysklearn
DEVELOPING PYTHON PACKAGES
Let's practice!
D E V E L O P I N G P Y T H O N PA C K A G E S