Release a Python Package

Release a Python Package

Prepare the distribution

1. Setting up the package directory

  • Make a new directory with any name you like
  • Copy your Python source into a subfolder
  • Add four empty files
    • README
    • LICENSE.txt
    • MANIFEST.in
    • setup.py

2. Writing setup.py

 Python
from distutils.core import setup 

with open('README') as file: 
	readme = file.read() 
setup( 
	name = 'some_unique_name', 
	version = '2.0.0', 
	packages = ['sourcefoldername'],
	url='https://testpypi.python.org/pypi/some_unique_name/', 
	license = 'LICENSE.txt', 
	description = 'my fantasy package', 
	long_description=readme, 
	author='my_name', 
	author_email='my_email' )

3. Updating the README and LICENSE.txt files

Note that the new lines before each heading and the one after the keyword .. code:: python are important

Attack of the Orcs
==================

Introduction
-------------
This is a command line fantasy war game!

Documentation
--------------
Documentation can be found at...

Example Usage
-------------
Here is an example to import the modules from this package.

.. code:: python
from wargame.attackoftheorcs import AttackOfTheOrcs
game = AttackOfTheOrcs()
game.play()

LICENSE
-------
See LICENSE.txt file.

 

Step 4 – Updating the MANIFEST.in files

include *.txt

 

Step 5- Build a deployment-ready distribution

$ cd <project_folder>
$ python setup.py sdist

The sdist command creates a distribution with the source files included. Running the second command creates a new dist directory containing an archive file. For example, in setup.py, if the name field is testgamepkg and the version is 2.0.0, the archive will be testgamepkg-2.0.0.tar.gz on Linux and testgamepkg-2.0.0.zip on Windows OS

 

 

Uploading the distribution

1. Creating an account on PyPl test website

https://testpypi.python.org/pypi?:action=register_form

2. Creating a .pypirc file

[distutils]
index-servers=
pypitest

[pypitest]
repository = https://testpypi.python.org/pypi
username=<add username>
password=<add password>

3. Register your project

$ cd /path/to/testgamepkg
$ python setup.py register -r pypitest

Use other profile in .pypirc

$ python setup.py register -r https://testpypi.python.org/pypi

4. Uploading the package

$ python setup.py sdist upload -r pypitest

 

Installing your own distribution

$ pip install -i https://testpypi.python.org/pypi testgamepkg

Packaging and uploading the new version

$ python setup.py sdist upload -r pypitest