warbaque
warbaque

Reputation: 633

How should I write a simple installer for python package?

I'm writing a simple python package with many helper functions to be used with other projects. How should handle I packaging and make installer for said package?

Directory layout

package:

package (/git/dev/package)
 |
 +-- __init__.py
 |
 +-- foo.py
 |
 +-- bar.py

project using said package:

project (/git/dev/project)
 |
 +-- project.py

How do I make this package available to every local python project (I don't need to distribute it publicly)? Should installer add current package location to path or use some other way?

Current preferred workflow:

1. checkout package from version control
2. do something so python finds and can use that said package
3. use package in some project
4. edit package (project should use edited package even before I push those changes to repo)

File contents

project.py:

# Doesn't work currently since package isn't added to path
from package import foo
from package import bar

foo.do_stuff()
bar.do_things()

Upvotes: 0

Views: 90

Answers (1)

abarnert
abarnert

Reputation: 366133

How do I make this package available to every python project?

The standard way to distribute packages is as source distributions on PyPI (or a private pip-compatible repository). The details take up many pages, so I can't explain them all here, but the Python Packaging User Guide has everything you want to know.

The basic idea is that you create a setup.py file that tells Python how to install your program. Look at the official sample linked from the tutorial.

Now, someone can just download and unzip your package and run python setup.py install and it will install your program for them. Or, better, they can type pip install ..

But, even better, you can python setup.py sdist to create a source distribution, test it out, upload it to PyPI, and then users don't have to download anything, they can just pip install mylib.

If you want binary installations for different platforms, setuptools knows how to make both Windows installers, which can be double-clicked and run, and wheel files, which can be installed with pip (in fact, it'll automatically find and use wheels instead of source distributions). If you don't have any compiled C extensions, python setup.py bdist_wheel --universal is enough to build a wheel that works everywhere.


Should installer add current package location to path or use some other way?

The installer should install the package into the user's system or user (or current-virtual-environment-system) site-packages. If you use setuptools, pip will take care of this automatically.


If you're not trying to make this public, you can still use setuptools and pip. Just check the source distribution into source control, then you can install the latest version at any time like this:

pip install --upgrade git+https://github.com/mycompany/mylib

This also means you can skip a lot of the PyPI metadata in your setup.py file (e.g., nobody cares about your project's classifiers if it's not going to end up in the PyPI repository).

But you can still take advantage of all of the parts of the Python packaging system that make your life easier, while skipping the parts that aren't relevant.

Upvotes: 2

Related Questions