Absolute imports?
Terry Reedy
tjreedy at udel.edu
Sat Jan 8 19:32:12 EST 2011
On 1/8/2011 5:48 PM, Roy Smith wrote:
> In article<mailman.658.1294522447.6505.python-list at python.org>,
> Terry Reedy<tjreedy at udel.edu> wrote:
>
>>
>> Import from another file in /home/roy. (since '.' is part of sys.path).
>> Or put module or package of modules in Lib/site-packages.
>> But why the horror of modifying sys.path? It is normal proceedure.
>
> Not quite horror, but since I already know the absolute path to the
> file, it seems silly to change the search path just so import can use it
> to search. Also, if I change the search path, I risk other things
> finding my module by mistake (if there's a module name collision).
Ben Finney asked you the right question. If config.py is the only .py
file in .../autogen/, which I infer from your responses, you could
.pop() .../autogen after the append and import. Or open config.py and
use imp.load_module, being careful to get all the args correct. In
either case, if I had a startup module or utility module that is
imported right away, I would do the import with extra code there, just
once. Then, either refer to util.config after importing util in other
modules, or just do 'import config'. The latter should work because
import first looks for already imported modules (in sys.modules). When
the module is already there, 'import x' reduces to "x = sys.modules['x']".
--
Terry Jan Reedy
More information about the Python-list
mailing list