Posted by Mark Withall: 2016-12-20

The following assumes Python 3.x on Windows and pip version 8.1.1 or earlier

Creating A Module

Required files:

  • setup.py
  • setup.cfg
  • README
  • MANIFEST.in

and a folder containing the module itself.

NOTE: README can be one of: README, README.txt or README.rst

All files can be blank except for setup.py.

Minimal content of setup.py:

from setuptools import setup, find_packages

setup(name='modulename',
      version='0.1.0',
      description='Put description here',
      author='Name',
      author_email='name@example.com',
      url='module url',
      packages=find_packages(exclude=['docs', 'tests']))

To build the module package, run the command: python setup.py sdist from the root of the project.

This will create a folder dist with the file modulename-0.1.0.zip

For more details see the documentation.

Using external version.txt

To read the version number from a file:

from setuptools import setup, find_packages

with open('version.txt') as f:
    version = f.readline().rstrip()

setup(name='modulename',
      version=version,
      description='Put description here',
      author='Name',
      author_email='name@example.com',
      url='module url',
      packages=find_packages(exclude=['docs', 'tests']))

NOTE from pip verison 8.1.2 onwards, the modulename should replace dots with hyphens; otherwise pip won’t be able to find them on the self-hosted server

version.txt will also need to be added to the MANIFEST.in file:

include version.txt

Hosting A Repository

To host the server, put the modulename-0.1.0.zip file into a folder archive\modulename, then run python -m http.server 9000 from the archive folder.

For more details see the documentation and also.

Installing The Module

pip install --extra-index-url http://localhost:9000/ modulename

It’s probably worth doing this in a virtual environment to avoid corrupting the main python installation:

> python -m venv venv
> venv\Scripts\activate.bat
> pip install --extra-index-url http://localhost:9000/ modulename
> ... do stuff ...
> deactivate