Application and package of the same name

Paul Moore p.f.moore at gmail.com
Thu Oct 19 14:34:56 EDT 2017


On 19 October 2017 at 19:18, Skip Montanaro <skip.montanaro at gmail.com> wrote:
> I'm not understanding something fundamental about absolute/relative
> imports. Suppose I have an application, fribble.py, and it has a
> corresponding package full of goodies it relies on, also named fribble.
> From the fribble package, the application wants to import the sandwich
> function from the lunchtime module. At the top level it thus has an import
> like this:
>
> from fribble.lunchtime import sandwich
>
> I might have a directory structure like this:
>
> example
>     example/fribble.py
> fribble
>         __init__.py
>         lunchtime.py
>
> If I run inside the example directory with PYTHONPATH=.. I can't
> find/import the fribble package, because the main application directory is
> prepended to sys.path. Consequently, the import machinery never gets any
> further down sys.path. It stumbles on the fribble application and tries to
> find the bits it's interested in, to no avail.
>
> This is in Python 2.7, FWIW. What am I missing?

My immediate reaction is "you shouldn't name your main program and
your package the same". It's not a pattern I've seen commonly used.

However, the approaches I've seen used (a __main__.py inside the
package, so you can execute it via `python -m fribble`, or a setup.py
entry point to generate a script wrapper for the application) may be
more common among people focused more on library development than on
application development.

Paul



More information about the Python-list mailing list