<div class="gmail_quote">On Tue, Feb 2, 2010 at 1:38 PM, Roel Schroeven <span dir="ltr"><<a href="mailto:rschroev_nospam_ml@fastmail.fm">rschroev_nospam_ml@fastmail.fm</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Apparently, contrary to my expectations, Python looks in the directory<br>
containing the currently running script instead. That means that the<br>
behavior of "import foo" depends very much on circumstances not under<br>
control of the module in which that statement appears. Very fragile.<br>
Suggestions to use better names or just poor workarounds, IMO. Of the<br>
same nature are suggestions to limit the amount of scrips/modules in a<br>
directory... my /usr/bin contains no less than 2685 binaries, with 0<br>
problems of name clashes; there is IMO no reason why Python should<br>
restrict itself to any less.<br>
<br>
Generally I like the design decisions used in Python, or at least I<br>
understand the reasons; in this case though, I don't see the advantages<br>
of the current approach.</blockquote><div><br></div><div>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.</div>
<div><br></div><div>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.</div><div><br></div><div><a href="http://www.python.org/dev/peps/pep-0328/">http://www.python.org/dev/peps/pep-0328/</a></div>
<div><br></div><div>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 path. <br></div><div><br></div><div>In 2.6, relative imports of modules in the same dir (thus, possible shadowing modules) raises a deprecation warning.</div>
<div> </div><div>In Python 3+, you have to use the explicit syntax to get at modules in the current directory.</div><div><br></div><div>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. </div>
<div><br></div><div>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.</div>
<div><br></div><div>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 :)</div><div><br></div>
</div>
<div name="mailplane_signature">--S</div>