FAQ
I have several files:

a.test.py b.test.py c.test.py

which carry out regression tests for their respective modules (a.py, b.py,
c.py). I would like to create an overall module called alltests.py.
However, when I try to do:

import a.test

in alltests.py, Python gets all huffy since "." is its separator for
module hierarchy.

So, how do I import a module with "." in its filename?

For both practical and political reasons, renaming a.test.py to a_test.py
is not an option.

-a

Search Discussions

  • Skip Montanaro at Jul 31, 2002 at 2:25 am
    Andrew> So, how do I import a module with "." in its filename?

    Take a look at the execfile builtin function.

    --
    Skip Montanaro
    skip at pobox.com
    consulting: http://manatee.mojam.com/~skip/resume.html
  • Duncan Booth at Jul 31, 2002 at 8:37 am
    "Andrew P. Lentvorski" <bsder at mail.allcaps.org> wrote in
    news:mailman.1028080249.16165.python-list at python.org:
    So, how do I import a module with "." in its filename?
    fname = 'c:/test/a.test.py'
    f = file(fname, 'r')
    a_test = imp.load_module('a_test', f, fname, ('.py', 'r', imp.PY_SOURCE))
    f.close()

    See the module imp documentation for further details.

    --
    Duncan Booth duncan at rcp.co.uk
    int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
    "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
  • Keith Dart at Jul 31, 2002 at 11:01 am
    In article <Xns925C612DE2B4Aduncanrcpcouk at 127.0.0.1>, "Duncan Booth"
    <duncan at nospamrcp.co.uk> penned these words:
    "Andrew P. Lentvorski" <bsder at mail.allcaps.org> wrote in
    news:mailman.1028080249.16165.python-list at python.org:
    So, how do I import a module with "." in its filename?
    fname = 'c:/test/a.test.py'
    f = file(fname, 'r')
    a_test = imp.load_module('a_test', f, fname, ('.py', 'r',
    imp.PY_SOURCE)) f.close()

    See the module imp documentation for further details.
    But.... why bother? Why not just rename the original modules to
    a_test.py, etc.? That's what I do.



    --
    -- ^
    \/ \/
    (O O)
    -- --------------------oOOo~(_)~oOOo----------------------------------------
    Keith Dart
    <mailto:kdart at kdart.com>
    <http://www.kdart.com/>
    ----------------------------------------------------------------------------
    Public key ID: B08B9D2C Public key: <http://www.kdart.com/~kdart/public.key>
    ===========================================================================
  • Duncan Booth at Jul 31, 2002 at 11:19 am
    "Keith Dart" <kdart at kdart.com> wrote in
    news:xsP19.36095$Zw6.135530681 at newssvr21.news.prodigy.com:
    But.... why bother? Why not just rename the original modules to
    a_test.py, etc.? That's what I do.
    Well, the OP said:
    For both practical and political reasons, renaming a.test.py to a_test.py
    is not an option.
    I'm not entirely sure I believe there are practical reasons, but the
    politics may well be insurmountable.

    --
    Duncan Booth duncan at rcp.co.uk
    int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
    "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
  • Peter Hansen at Jul 31, 2002 at 12:21 pm

    Duncan Booth wrote:
    "Keith Dart" <kdart at kdart.com> wrote in
    news:xsP19.36095$Zw6.135530681 at newssvr21.news.prodigy.com:
    But.... why bother? Why not just rename the original modules to
    a_test.py, etc.? That's what I do.
    Well, the OP said:
    For both practical and political reasons, renaming a.test.py to a_test.py
    is not an option.
    I'm not entirely sure I believe there are practical reasons, but the
    politics may well be insurmountable.
    I have to say if I worked in a place where renaming files became a
    loaded political issue, I would be leaving in a hurry. Like, the
    next day! That's insane.

    -Peter

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 31, '02 at 1:49a
activeJul 31, '02 at 12:21p
posts6
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase