UDACITY/AWS DEVELOPING A PYTHON PACKAGE LESSON
CREATING A PYTHON PACKAGE
A Python package is a folder bearing a set of modules with an __init__.py file. Python packages are housed in an online repository called PyPy. After creating a package, for it to be accessible to others we must upload it to PyPy online repository. The __init__.py file tells Python that the folder is a package. It could be empty or may contain code that is run each time a Python package is imported into a Python program. Example of code that may be inside the an __init__.py file is:
from .GaussianDistribution import Gaussian
Above is importing the Gaussian class from a file i.e. a module called GaussianDistribution, what this does is that, it allows us to use the Gaussian class in our programs by importing it using:
from distributions import Gaussian
Rather than the long statement:
from distributions.GaussianDistribution import Gaussian
The file structure being:
Python_Package
distributions
GaussianDistribution.py
BinomialDistribution.py
GeneralDistribution.py
README.md
__init__.py
setup.py
i.e. we have a folder ‘Python_Package’, with another folder ‘distributions’ and ‘setup.py’ file, with the distributions folder containing files ‘GaussianDistribution’, ‘BinomialDistribution’ and ‘__init__.py’ file.
setup.py is used by pip for pip installing a package. Pip automatically looks for this file whenever we use pip to install a package, hence every package must have its own setup.py file.
What is Pip? Pip is the default Python package manager. What is a package manager? A package manager is a piece of software that comes with a programming language interpreter that is chiefly responsible for managing how third party libraries used in developing software using the language are installed and uninstalled.
A possible content for the setup.py file is:
setup(name = ‘distribution’,
version = ‘0.1’
description=’Gaussian and Binomial Distributions’
packages=[‘distributions’],
zip-safe=False)
Above are metadata about the package, i.e. setup.py contains metadata about the package.
To install your new package locally, open command prompt and navigate to the directory containing the package, type:
pip install .
The . tells python that the setup.py is contained in the directory you are at in the command prompt, so make sure you are at the directory bearing the setup.py file.
After installing the package, to know where it is installed on your PC, type these two commands:
import distributions
distributions.__file__
VIRTUAL ENVIRONMENTS
Virtual Environments are like a virtual computer wherein we install a different version of software different from the version installed on the computer to test a code on that version. Example, a virtual environment can be created to test our code on Python3.6 whereas the Python version on our computer is Python 3.10.
Virtual Environments are used in package development. After creating your package, and you need to test it on your local machine, instead of installing it on your main Python installation on your computer, you can install it on the virtual environment.
The two Python environment managers are conda and venv. Conda manages packages and environments. Venv is just an environment manager that comes preinstalled with Python 3, where pip is the package manager.
To create a virtual environment with Venv, you enter the command below in the command prompt:
python3 –m venv environment_name
To activate the environment, you enter the command:
source environment_name/bin/activate
Get Opara Febechukwu C.’s stories in your inbox
Join Medium for free to get updates from this writer.
For Python software development we use pip and venv and for data science projects we use conda.
NOTE: When a python virtual environment is created in a directory, a folder bearing the environment name is created, and the folder contains a Python installation for running codes created in that environment.
PUTTING YOUR PACKAGE ON PYTHON PACKAGE INDEX (PYPI)
PyPi website has a test repository and the regular repository. The url for the test repository is https://test.pypy.org and the regular repository is https://pypi.org
Our package is first put on the test repository and tested and if everything works as expected, we then put it on the regular repository. The steps for uploading a package to Python Package Index is as follows:
Step 1: Create an account on https://test.pypy.org and https://pypi.org.
Step 2: Add a license.txt and setup.cfg file to your package at the root folder of the python package i.e.
i.e it would be at root folder of the package example:
Python_Package
distributions
GaussianDistribution.py
BinomialDistribution.py
GeneralDistribution.py
README.md
__init__.py
license.txt
setup.cfg
license.txt contains the copyright information.
Step 3: Update the setup.cfg file with the name of your README.md file e.g.
[metadata]
description-file = README.md
Step 4: Type any of the commands below in the command prompt:
python setup.py sdist
OR
python3 setup.py sdist bdist_wheel
Ensure that you are at the root folder of the package in the command prompt before entering the command. The above commands adds a .tar.gz file to the root folder and that is the file to be uploaded to Pypi and Pypy
Step 5: Install the twine package, using the command:
pip install twine
Step 6: Upload to Pypi test repo using the command:
twine upload — repository-url https://test.pypi.org/legacy/dist/*
Step 7: Enter the username and password for the test pypi website
Step 8: Test the package and see if you can install from the test repository using the command:
pip install — index-url https://test.pypi.org/simple/Python_example
Step 9: Test your installation, if everything is ok, the upload to the regular pypy repository using the command:
twine upload dist/*
Step 10: Enter your password and username for the regular pypy website
Step 11: Run the command:
pip uninstall Python_Package
This is to uninstall the package installation from the test repository and install from the regular pypy repository to test that the code uploaded there is working as seen in the test repository.
Step 12: Install from the regular Python website using the command
pip install Python_Package






