<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Your way looks fine to me.<br>
<br>
I think the PEP is a bit oversimplistic about imports.&nbsp; I think there
are at least a couple of really good reasons to put imports into the
code line rather than at the top of the file.<br>
<ul>
  <li>in line, the imports are easier to move along with the code when
refactoring.&nbsp; Done at the top of the file, you essentially end up
moving the code, then bumping into the missing imports one at a time
until you get them all.&nbsp; And imports which are no longer used, (the
ones in the old file), may never be removed which is misleading to the
point of being confusing as well as potentially impacting performance.<br>
  </li>
  <li>time.&nbsp; It takes essentially zero time to import an already
imported module.&nbsp; So importing multiple times is cheap.&nbsp; However, the
first import takes time - time that we may not need to spend.&nbsp; If we
delay that cost, we may never need to pay it.</li>
</ul>
Personally, I've often been putting many of my imports immediately
prior to the first use of the thing being imported.&nbsp; But when I have to
write an import a second time, I usually move the first import up in
scope to cover both usages.&nbsp; I'll also move an import up in scope if it
makes the code more readable or moves it out of a loop or other
repeated code.<br>
<br>
I can see advantages to top level imports and I use them too for the
most common imports, (like sys), or for things that I know are going to
be used pervasively throughout a file.&nbsp; I haven't come to a completely
comfortable balance for myself between using imports inline and placing
them at the top of the file.&nbsp; I suspect I will gravitate towards inline
for early development and things that can potentially be left
unimported and gravitating towards top-of-file as the code matures.<br>
<br>
--rich<br>
<br>
Glen Jarvis wrote:
<blockquote
 cite="mid:e5660d770910301040n39eb66e0xaf6ff0b4f2907c07@mail.gmail.com"
 type="cite">
  <div>Because I've *always* learned something from this community this
when posting similar questions, I have the following snippet ..</div>
  <div><br>
  </div>
  <div>This project is imported on all machines. The organization is
one module with reasonably small python files organized logically
(backup.py, diskspace.py, shared.py, webserver.py, etc.) These are menu
driven management scripts for someone doing operations.</div>
  <div><br>
  </div>
  <div>In my backup.py file, I have the following imports (organized in
groups per PEP-8; <a moz-do-not-send="true"
 href="http://www.python.org/dev/peps/pep-0008/">http://www.python.org/dev/peps/pep-0008/</a>).
However, one of our hosts intentionally doesn't have psycopg2
installed. How do most deal with the conditional import, while still
keeping the imports grouped at the top of the file? Or, better yet, how
do you deal with this? It's very tempting to break convention and only
import psycopg2 module in the functions that need it. That seems
extreme for just one node in our system, however.</div>
  <div><br>
  </div>
  <div>The following feels messy to me... I've been living with it, but
then, I betcha someone at BayPIGgies has a good suggestion :)</div>
  <div><br>
  </div>
  <div>from datetime import datetime, timedelta</div>
  <div>import os</div>
  <div>import subprocess</div>
  <div>import sys</div>
  <div>import tarfile</div>
  <div>from optparse import OptionParser</div>
  <div># There is no psycopg2 on host '[snip]'</div>
  <div>try:&nbsp;</div>
  <div>&nbsp;&nbsp; &nbsp;from psycopg2 import connect&nbsp;</div>
  <div>except ImportError:&nbsp;</div>
  <div>&nbsp;&nbsp; &nbsp;pass</div>
  <div><br>
  </div>
  <div>from myproject import other_modules # you get the idea</div>
  <div><br>
  </div>
  <div><br>
  </div>
  <div><br>
  </div>
  <pre wrap="">
<hr size="4" width="90%">
_______________________________________________
Baypiggies mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Baypiggies@python.org">Baypiggies@python.org</a>
To change your subscription options or unsubscribe:
<a class="moz-txt-link-freetext" href="http://mail.python.org/mailman/listinfo/baypiggies">http://mail.python.org/mailman/listinfo/baypiggies</a></pre>
</blockquote>
</body>
</html>