Absolute imports?

Discussion in 'Python' started by Roy Smith, Jan 8, 2011.

  1. Roy Smith

    Roy Smith Guest

    If I have an absolute path to a file (i.e. '/home/roy/foo.py'), is there
    a way to import that as a module WITHOUT modifying sys.path? I'm using
    Python 2.6.

    I've read PEP 328, and don't really understand how the absolute imports
    it's talking about are supposed to work. Should I be using
    imp.load_source()?
    Roy Smith, Jan 8, 2011
    #1
    1. Advertising

  2. Roy Smith

    Terry Reedy Guest

    On 1/8/2011 3:03 PM, Roy Smith wrote:
    > If I have an absolute path to a file (i.e. '/home/roy/foo.py'), is there
    > a way to import that as a module WITHOUT modifying sys.path? I'm using
    > Python 2.6.


    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.

    > I've read PEP 328, and don't really understand how the absolute imports
    > it's talking about are supposed to work.


    Those are the normal imports that start from a directory in sys.path.
    Relative imports (now) are ones that use '.'s to locate relative to the
    importing module. I have never done that. Purpose is to make a
    subpackage relocatable to another package without modification.

    > Should I be using imp.load_source()?


    No idea, never done that. Try it if you want.

    --
    Terry Jan Reedy
    Terry Reedy, Jan 8, 2011
    #2
    1. Advertising

  3. Roy Smith

    Roy Smith Guest

    In article <>,
    Ben Finney <> wrote:

    > Roy Smith <> writes:
    >
    > > If I have an absolute path to a file (i.e. '/home/roy/foo.py'), is
    > > there a way to import that as a module WITHOUT modifying sys.path? I'm
    > > using Python 2.6.

    >
    > Importing a module with ‘import’ is done by using the module's name,
    > which is only *incidentally* related to its filesystem path.
    >
    > What is the problem you're trying to solve? It is likely we can suggest
    > a better solution.


    Well, the problem I'm trying to solve is that I have an absolute
    pathname to a python source file that I want to import as a module :)

    I'm working on a project where developers will typically have several
    sandboxes, with different versions of the code. There's a config file
    (which I have no control over) which defines a big multi-level dict
    containing configuration options:

    config = {
    'memcache_servers' : ['localhost'],
    'build_type' : 'development',
    'thrift_configs' : {
    'SearchService' : {'hosts' : ['localhost'],
    'sendTimeout' : 300,
    'port' : 9192,
    'recvTimeout' : 3000
    },
    }
    }

    and so on. The file is auto-generated by some huge pile of perl scripts
    which also generates the same information in a forms ingestible by perl,
    PHP, Java, shell scripts, etc. Not how I would have designed it, but it
    is what it is.

    The best I can describe how to find the location of the config file is,
    "Work your way up the directory tree from where you are now, (i.e.
    following '..' links) until you get to the top level of the project,
    then from there, it's ./code/configs/autogen/config.py."

    It's reasonably straight-forward to figure out that absolute path,
    starting from sys.argv[0] and using the tools in os.path. Now I need to
    import the file, given that I know its absolute pathname. It looks like
    imp.load_source() does what I want, I'm just wondering if there's a
    cleaner way.
    Roy Smith, Jan 8, 2011
    #3
  4. Roy Smith

    Roy Smith Guest

    In article <>,
    Terry Reedy <> 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).
    Roy Smith, Jan 8, 2011
    #4
  5. Roy Smith

    Terry Reedy Guest

    On 1/8/2011 5:48 PM, Roy Smith wrote:
    > In article<>,
    > Terry Reedy<> 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
    Terry Reedy, Jan 9, 2011
    #5
  6. Roy Smith wrote:
    > [snip]
    > It's reasonably straight-forward to figure out that absolute path,
    > starting from sys.argv[0] and using the tools in os.path. Now I need to
    > import the file, given that I know its absolute pathname. It looks like
    > imp.load_source() does what I want, I'm just wondering if there's a
    > cleaner way.
    >


    What about

    config = __import__(configPath.replace('.py', ''))


    JM
    Jean-Michel Pichavant, Jan 10, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Cooper

    Absolute Imports and PYTHONPATH

    Richard Cooper, May 5, 2004, in forum: Python
    Replies:
    1
    Views:
    427
    John Roth
    May 6, 2004
  2. Replies:
    0
    Views:
    318
  3. Albert
    Replies:
    4
    Views:
    10,832
    Albert
    Jul 10, 2008
  4. zildjohn01
    Replies:
    0
    Views:
    629
    zildjohn01
    Feb 22, 2011
  5. Victor Hooi
    Replies:
    1
    Views:
    98
    Devin Jeanpierre
    Nov 25, 2013
Loading...

Share This Page