Skip to content

Instantly share code, notes, and snippets.

@ShawnClake
Forked from gboeing/pypi.md
Last active June 2, 2018 17:19
Show Gist options
  • Save ShawnClake/759e9d09af868ef18f8c7b39d1684ad4 to your computer and use it in GitHub Desktop.
Save ShawnClake/759e9d09af868ef18f8c7b39d1684ad4 to your computer and use it in GitHub Desktop.
How to organize and distribution a package on pypi

To distribute a package on pypi

Directory structure

/project/
    /package/
        __init__.py
        module.py
    setup.py

File contents

/project/package/__init__.py

from .module import *
__version__ = '0.1'

/project/package/module.py

# this file just contains your module's code

/project/setup.py

from setuptools import setup
setup(name='module',
      version='0.1',
      description='What the module does',
      url='https://github.com/username/repo',
      author='Your Name',
      author_email='[email protected]',
      license='MIT',
      packages=['module'],
      install_requires=['numpy>=1.11',
                        'matplotlib>=1.5'])

For packages with many submodules

from setuptools import setup, find_packages
setup(
  name = 'apitax',
  packages = find_packages(), # this must be the same as the name above
  version = '2.0.0',
  description = 'Apitax combines the power of Scriptax and COmmandtax into a quick and easy to use Python package to facillitate powerful Restful API Request Scripting',
  author = 'Shawn Clake',
  author_email = '[email protected]',
  url = 'https://github.com/ShawnClake/Apitax', # use the URL to the github repo
  #download_url = '', # I'll explain this in a second
  keywords = ['restful', 'api', 'commandtax', 'scriptax'], # arbitrary keywords
  classifiers = [],
  install_requires = [
    'click',
    'bottle',
    'requests',
    'antlr4-python3-runtime',
  ],
)

Set up pypi

Create a file in the home directory called ~/.pypirc with contents:

[distutils]
index-servers = pypi

[pypi]
repository = https://pypi.python.org/pypi
username = YourPyPiUsername
password = YourPyPiPassword

Build, register, and upload to pypi

Open terminal window and change directory to /project/

Then run setup.py with sdist to build a source distribution and bdist_wheel to build a wheel (with --universal flag if your package is Python 2/3 universal). Then use twine to register it and upload to pypi.

python setup.py sdist bdist_wheel --universal
twine register dist/project-x.y.z.tar.gz
twine register dist/mypkg-0.1-py2.py3-none-any.whl
twine upload dist/*

Build and upload subsequent updates to pypi

Update the change log and edit the version number in setup.py and package/__init__.py.

Open terminal window and change directory to /project/ then run setup.py with sdist to build a source distribution and bdist_wheel to build a wheel (with --universal flag if your package is Python 2/3 universal). Remove old versions from /project/dist/ and then use twine to upload to pypi.

python setup.py sdist bdist_wheel --universal
twine upload dist/*

Release your code on GitHub

To tag your current commit as a released version, run:

git tag -a v0.1 -m "annotation for this release"
git push origin --tags

Release your code on conda-forge

If you already have a conda-forge feedstock forked to your own GitHub account, first edit recipe/meta.yaml to update the version, hash, etc. To calculate the sha256 hash, run:

openssl dgst -sha256 path/to/package_name-0.1.1.tar.gz

Then, commit and push the yaml file to GitHub:

git pull upstream master
git add --all
git commit -m 'version bump to v0.1.1'
git push -u origin master

Finally, issue a pull request to conda-forge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment