James McMahon
James McMahon

Reputation: 49629

Packaging python programs with a command line script

I've trying to get my pet Python project into a releasable state and I'm stumped when it comes to packaging it.

Mark Pilgrim recommend this directory structure in Dive Into Python

httplib2/                 
|  
+--README.txt             
|  
+--setup.py               
|  
+--httplib2/              
    |  
    +--__init__.py  
    |  
    +--iri2uri.py

What I can't figure out is, if I have a runner script ie an executable command line program, say foo and the name of my project is foo, what should I name the internal package directory?

To give a concrete example, if I have

README.md
LICENSE
somefile1.py
somefile2.py
foo

What is that best way to package this?

For instance

+--README.md
|
+--LICENSE
|
+--foo
|
+--foo/
    |
    +--somefile1.py
    |
    +--somefile2.py

Doesn't work because of the duplicate name.

Upvotes: 3

Views: 456

Answers (1)

Evan Plaice
Evan Plaice

Reputation: 14140

You're doing it wrong...

Here's what the structure 'should' look like

foo 1.0/
| +--README.txt
| +--setup.py
| +--foo/__init__.py
| +--foo/iri2uri.py
| +--foo/httplib2/__init__.py
| +--foo/httplib2/bar.py

Think of the outer, enclosing folder as the package. This should include installation and documentation files LICENSE, README, MANIFEST, setup.py. The folders within the package (in this case, just '/foo') are the modules.

To access all of the functions above you'd use the following import statements:

import foo                # represented by foo/__init__.py
import foo.iri2uri        # represented by foo/iri2uri.py
import foo.httplib2       # represented by foo/httplib2/__init__.py
import foo.httplib2.bar   # represented by foo/httplib/bar.py

Technically, you can choose to include sub-modules as either a file or a folder. Folders are only necessary of there are sub-modules of the sub-module.

For instance:

foo 1.0/
| +--/foo/iri2uri.py
| +--/foo/iri2uri/__init__.py

Will be both be interpreted the same way.

For example:

import foo.iri2uri

It's a little tricky at first but everybody who has ever built a package for installation has encountered this speed bump.

Update: I think this better answers your question

For executables you create a separate package

No installer is necessary because this code won't be imported by other applications.

examples/
| +--/foo.py

They should be run as stand-alone programs and import the necessary modules from the library (foo) that you installed.

Upvotes: 3

Related Questions