Packaging Your Code

Packaging your code is important.

You’ll need to package your code first before sharing it with other developers.

The Python Packaging Guide provides an extensive guide on creating and maintaining Python packages.

For Python Developers

If you’re writing an open source Python module, PyPI , more properly known as The Cheeseshop, is the place to host it.

Pip vs. easy_install

Use pip. More details here

Personal PyPI

If you want to install packages from a source other than PyPI, (say, if your packages are proprietary), you can do it by hosting a simple http server, running from the directory which holds those packages which need to be installed.

Showing an example is always beneficial

For example, if you want to install a package called MyPackage.tar.gz, and assuming this is your directory structure:

  • archive
    • MyPackage
      • MyPackage.tar.gz

Go to your command prompt and type:

$ cd archive
$ python -m SimpleHTTPServer 9000

This runs a simple http server running on port 9000 and will list all packages (like MyPackage). Now you can install MyPackage using any Python package installer. Using Pip, you would do it like:

$ pip install --extra-index-url=http://127.0.0.1:9000/ MyPackage

Having a folder with the same name as the package name is crucial here. I got fooled by that, one time. But if you feel that creating a folder called MyPackage and keeping MyPackage.tar.gz inside that, is redundant, you can still install MyPackage using:

$ pip install  http://127.0.0.1:9000/MyPackage.tar.gz

pypiserver

Pypiserver is a minimal PyPI compatible server. It can be used to serve a set of packages to easy_install or pip. It includes helpful features like an administrative command (-U) which will update all its packages to their latest versions found on PyPI.

S3-Hosted PyPi

One simple option for a personal PyPi server is to use Amazon S3. A prerequisite for this is that you have an Amazon AWS account with an S3 bucket.

  1. Install all your requirements from PyPi or another source
  2. Install pip2pi
  • pip install git+https://github.com/wolever/pip2pi.git
  1. Follow pip2pi README for pip2tgz and dir2pi commands
  • pip2tgz packages/ YourPackage (or pip2tgz packages/ -r requirements.txt)
  • dir2pi packages/
  1. Upload the new files
  • Use a client like Cyberduck to sync the entire packages folder to your s3 bucket
  • Make sure you upload packages/simple/index.html as well as all new files and directories
  1. Fix new file permissions
  • By default, when you upload new files to the S3 bucket, they will have the wrong permissions set.
  • Use the Amazon web console to set the READ permission of the files to EVERYONE.
  • If you get HTTP 403 when trying to install a package, make sure you’ve set the permissions correctly.
  1. All done
  • You can now install your package with pip install --index-url=http://your-s3-bucket/packages/simple/ YourPackage

For Linux Distributions

Useful Tools