Publish and Packaging Python project - 1

If we want to publish a python software and let other people to use our tool as well, then we need to package our code files and than publish it.

Layout of package

packaging_tutorial
|———— LICENSE
|———— README
|———— example_pkg
| |________ _init _.py
|———— setup.py
|———— test
Once we create this structure, we need to cd packaging_tutorial

  • Project structure needs to be in place. Usually, the root of the repository contains a folder with the name of the repository — this is where the core project code should be. What’s outside of this folder is the extra code that’s necessary to run and build the package (tests, documentation, etc.)
  • The core folder should include one (or more) module(s) and an _init_.py file which imports the classes/functions you’ll want the end user to have access to. This file can also include the version of the package, to make it easily accessible to the end user.
  • example_pkg : _init _.py files are required to make Python treat directories containing the file as packages. _init_.py can just be an empty file, more info please check : python packeges and modules
  • test folder : containg all unit test files, we can leav it empty now.

Choose license

The usual licenses for open sources projects are GNU, MIT, BSD, detalied see License

README file

A README is a txt file that contains informations which is commonly required to understand what the project is about. The most common format is Markdown
I usually use online readme.md editor Dillinger
Instruction to write a beautiful README.md please check my another article and here is an example of my readme.md
Example of README.md

Create a Setup.py file

from setuptools import setup
def parse_requirements(requirements):
    """
    Returns the list of requirements found in the requirements file
    """
    with open(requirements, "r") as req_file:
        return [l.strip('\n') for l in req_file if l.strip('\n')
                and not l.startswith('#')]
#pacages: each folder that with a __init__.py file in its dir, here contigs_alg is a #subofolder under raccroche with __init__.py
packages = ['raccroche', 'raccroche.contigs_alg'] 
requires = parse_requirements("requirements.txt")
#scripts 
scripts = ['bin/raccroche_main'] 

classifiers = [
    "Environment :: Console",
    "Intended Audience :: Science/Research",
    "License :: OSI Approved :: BSD License",
    "Programming Language :: Python :: 3",
    "Operating System :: OS Independent",
    "Topic :: Scientific/Engineering :: Bio-Informatics",
]

with open('README.md') as f:
    long_description = f.read()

setup(
    name='raccroche',
    packages=packages,
    version='1.0.0',
    description='raccroche demo: Reconstruction of AnCestral COntigs and CHromosomEs',
    long_description=long_description,
    author='Qiaoji Xu, Lingling Jin',
    author_email='qxu062@uottawa.ca, lingling.jin@cs.usask.ca',
    license='BSD-2-Clause',
    platforms='OS Independent',
    package_data={'': ['LICENSE']},
    download_url='https://github.com/Qiaojilim/raccroche_demo',
    url='https://github.com/Qiaojilim/raccroche_demo',
    scripts=scripts,
    include_package_data=True,
    install_requires=requires,
    #tests_require=['pytest'],
    #cmdclass={'tests': PyTest},
    classifiers=classifiers,
    python_requires='>=3'
)

Notes:
If you want metadata (from the repository) to be downloaded when anyone installs the package, you should add these through the package_data argument.

Generating distribution archives

The next step is to generate distribution packages for the package. These are archives that are uploaded to the Package Index and can be installed by pip.

Make sure you have the latest versions of setuptools and wheel installed:

python3 -m pip install --user --upgrade setuptools wheel

Now run this command from the same directory where setup.py is located:
python3 setup.py sdist bdist_wheel

This command should output a lot of text and once completed should generate two files in the dist directory:

dist/
  example_pkg_YOUR_USERNAME_HERE-0.0.1-py3-none-any.whl
  example_pkg_YOUR_USERNAME_HERE-0.0.1.tar.gz

Local installation

In dist folder, decompress the zip example_pkg_YOUR_USERNAME_HERE-0.0.1.tar.gz
and cd /this /decompressed /folder /where /the /setup.py /locate
run: sudo python3 setup.py install
We install it in our local dir /Library/Python/3.8/site-packages

Uninstall

sudo pip3 uninstall program_name

References links:
https://medium.com/free-code-camp/from-a-python-project-to-an-open-source-package-an-a-to-z-guide-c34cb7139a22
https://www.jianshu.com/p/b19330b9ab14