Full example can be found here: https://github.com/AntheaJFW/setuptools-example
We will want to start with a fresh installation of python to test whether the module works from a fresh install. Hence, create and activate virtual environment with python3:
python3 -m venv venv
source venv/bin/activate
For this example, we’re using the following CLI example, Order Pizza, which will require pyinquirer:
pip install pyinquirer
Install Setup tools, as we will need this to make this into a pip install-able package:
pip install --upgrade setuptools
Creating module to import:
Create a file order like so:
order_pizza/
order_pizza/
__init__.py
order_pizza.py
setup.py
Where order_pizza.py will have the function OrderPizza to be used to run the cli app, and __init__.py
will only contain the following:
from .order_pizza import OrderPizza
Hence when order_pizza is imported, OrderPizza is in the module namespace.
This means, if we import order_pizza, we can now also allow calling OrderPizza() using the following:
import order_pizza
order_pizza.OrderPizza()
We can test the setup by starting python in the second level ie
order_pizza/
-> order_pizza/
__init__.py
order_pizza.py
setup.py
Starting python repl:
cd order_pizza
python
And trying to import and run the module with either of the following:
from order_pizza import OrderPizza
OrderPizza()
import order_pizza
order_pizza.OrderPizza()
In setup.py
, we can specify the modules that this module will depend on, by specifying these in install_requires
. In this case, as our module only has one external dependency, pyInquirer, we can specify this here:
from setuptools import setup
setup(name='OrderPizza',
version='0.1',
description='OrderPizza',
url='',
author='',
author_email='',
license='MIT',
packages=['order_pizza'],
zip_safe=False,
install_requires=[
'pyInquirer',
])
Check that it works using:
python setup.py develop
Sucess would output a message similar to the following will prompt:
Finished processing dependencies for OrderPizza==0.1
Then you may install the module using
pip install .
Test that it works by changing your terminal directory to elsewhere and running python, ie:
cd ~
python
Then import your new module!:
from order_pizza import OrderPizza
OrderPizza()
Congrats! Enjoy the packaged pizza!