How to guard against bugs like this one?

Stephen Hansen apt.shansen at
Tue Feb 2 23:43:07 CET 2010

On Tue, Feb 2, 2010 at 1:38 PM, Roel Schroeven <
rschroev_nospam_ml at> wrote:

> Apparently, contrary to my expectations, Python looks in the directory
> containing the currently running script instead. That means that the
> behavior of "import foo" depends very much on circumstances not under
> control of the module in which that statement appears. Very fragile.
> Suggestions to use better names or just poor workarounds, IMO. Of the
> same nature are suggestions to limit the amount of scrips/modules in a
> directory... my /usr/bin contains no less than 2685 binaries, with 0
> problems of name clashes; there is IMO no reason why Python should
> restrict itself to any less.
> Generally I like the design decisions used in Python, or at least I
> understand the reasons; in this case though, I don't see the advantages
> of the current approach.

This really isn't anything new, novel, or even interesting. Its been known
forever that Python searches the script's directory for other scripts,
there's reasons for this.

Its also been known forever that its not really an ideal situation, and so
over the last seven or so years, Python's been working on fixing it.

In 2.5, you could activate your modules to use absolute imports by default,
thus requiring you to use special syntax to access modules in your own

In 2.6, relative imports of modules in the same dir (thus, possible
shadowing modules) raises a deprecation warning.

In Python 3+, you have to use the explicit syntax to get at modules in the
current directory.

This has taken years to address, yeah, because touching the import machinery
is -dangerous-; you have to do it very carefully to be sure that vast
amounts of code doesn't break that's relying on the existing,
not-entirely-well-documented-or-defined mechanics.

Why was Python designed like this? Ask Guido. I don't know, but I'm not
surprised: python was always very /flat/ originally. Strict, flat scopes,
didn't even have packages, etc. Its slowly gotten a little more nested /
deeper over time-- from limited nested scoping (only for enclosing
functions), to now absolute imports being default.

Its a slow process seeking a delicate balance; "flat is better then nested"
vs "namespaces are one honking great idea" are somewhat contradictory, after
all :)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list