<div>Because I&#39;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 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&#39;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&#39;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&#39;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 &#39;[snip]&#39;</div><div>try: </div><div>    from psycopg2 import connect </div>
<div>except ImportError: </div><div>    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>