[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Ideal directory structure?



Umang <umang.me@gmail.com> writes:

> One alternative that I can think of is:
>
>     scripts/foo
>     foo/
>     foo/__init__.py
>     ...

This is what I generally do, except I follow Unix convention and use the
directory name ‘bin’ for a directory containing executable programs.

> Here I don't understand how I could have the following line in my code:
>
>     from foo include bar

First, don't name the module the name name as the program name; it will
only lead to confusion over what is being referred to. One convention
I've seen is for the library used by the application to be in a module
package named ‘foolib’, for example::

    bin/
        foo
    foolib/
        __init__.py
        wibble.py
        warble.py

This is a limit imposed by the fact that you can't easily import modules
by a full filesystem path in Python, so the names need to be
distinguishable in the context of the import search path.

As for getting access to ‘foolib’ for import: you need to make sure it's
on the import path somehow.

* For a library of general use, install it to the Python site packages.
  You can then write your application as just one of an arbitrary number
  of applications using that library from the Python public site
  modules::

      import foolib.wibble
      import foolib.warble

* For the more usual case where the library is only of interest in your
  specific application, install it to an application-specific location
  (generally somewhere under ‘/usr/share/foo/’) and explicitly add that
  filesystem path to the import search path::

      import sys

      sys.path.append(os.path.join('/', 'usr', 'share', 'foo', 'foolib'))
      import wibble
      import warble

Whichever you choose, actually *getting* the modules to the right place
is the job of a build system. Setuptools can be coaxed into doing this
job with difficulty; I don't know if others make it any easier.


An alternative way is to have no manually-written application program
file at all, and to use Setuptools “entry points” to generate at build
time a wrapper program for a “main function”. From the sound of it, that
would be the neatest (since it will work without manually managing
library locations), but I have no experience with it.

-- 
 \     “To stay young requires unceasing cultivation of the ability to |
  `\                   unlearn old falsehoods.” —Robert Anson Heinlein |
_o__)                                                                  |
Ben Finney


Reply to: