Blog

Learning by doing.


Publish Packages to Pypi

Jul 4, 2018 • Python • Tags: Python Pypi

Step 1. Create a minimal project structure

Take my project sLMR as an example, a minimal structrue should be like below:

.
├── bin  # this folder is only required when you want to include an executable script
│   └── slmr
├── slmr  # this folder is always required
│   └── __init__.py  # required
├── README.md  # optional but useful
├── setup.py  # required

Step 2. Edit setup.py

Now let’s look at the setup.py. It is not minimal but typical:

from setuptools import setup, find_packages

with open("README.md", "r") as fh:
    long_description = fh.read()

setup(
    name='slmr',  # required
    version='0.1.0',
    description="A scripting system for the Last Millennium Reanalysis (LMR) project.",
    long_description=long_description,
    long_description_content_type="text/markdown",
    author="Feng Zhu",
    author_email='[email protected]',
    url='https://github.com/fzhu2e/slmr',
    packages=find_packages(),
    include_package_data=True,
    license="MIT license",
    zip_safe=False,
    scripts=['bin/slmr'], 
    keywords='slmr',
    classifiers=[
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.6',
    ],
)

Step 3. Publish to Pypi

First, we need to register on Pypi. After that, we will use twine to generate distribution and upload to Pypi:

pip install twine  # for the first time, install this package
python setup.py sdist
twine upload dist/*  # prompt to type in username and password on Pypi

Step 4. Install the package

After the steps above, users may install the package from Pypi with

pip install slmr

As a developer, I will install the package locally with

# cd to the path of the project first, and then
pip install -e .  

This will create a soft link of the package to my PATH so that the latest modifications of the code will be immediately valid anywhere on my machine.

Tags: Python Pypi