import from environment path

Discussion in 'Python' started by Guillaume Martel-Genest, Jun 17, 2011.

  1. Hi,

    Here's my situation : I got a script a.py that need to call b.py. The
    2 scripts can't be in a same package. Script a.py knows the path of
    b.py relative to an environment variable B_PATH, let's say B_PATH/foo/
    b.py. The solution I found is to do the flowwing :

    b_dir = os.path.join(os.environ['B_PATH'], 'foo')
    sys.path.append(b_dir)
    import b
    b.main()

    Is it the right way to do it, should I use subprocess.call instead?
     
    Guillaume Martel-Genest, Jun 17, 2011
    #1
    1. Advertising

  2. Guillaume Martel-Genest

    Chris Torek Guest

    In article <>
    Guillaume Martel-Genest <> wrote:
    >Here's my situation : I got a script a.py that need to call b.py. The
    >2 scripts can't be in a same package. Script a.py knows the path of
    >b.py relative to an environment variable B_PATH, let's say B_PATH/foo/
    >b.py. The solution I found is to do the flowwing :
    >
    >b_dir = os.path.join(os.environ['B_PATH'], 'foo')
    >sys.path.append(b_dir)
    >import b
    >b.main()
    >
    >Is it the right way to do it, should I use subprocess.call instead?


    The "right" way depends on what you want to happen.

    Consider, e.g., the case where sys.path starts with:

    ['/some/where/here', '/some/where/there', ...]

    and program a.py lives in /some/where/here. Suppose B_PATH is
    '/where/b/is'. The sys.path.append will leave sys.path set to:

    ['/some/where/here', '/some/where/there', ..., '/where/b/is']

    If /some/where/there happens to contain a b.py, your "import b"
    will load /some/where/there/b.py rather than /where/b/is/b.py.

    Did you want that? Well, then, good! If not ... bad! :)

    Consider what happens if there is a bug in b.main(), or b.main()
    is missing entirely. Then "import b" works, but the call b.main()
    raises an exception directly in program a.py.

    Did you want that? Well, then, good! If not ... bad! :)

    You might also want to take a look at PEP 302:

    http://www.python.org/dev/peps/pep-0302/

    If you use "subprocess" to run program B, it cannot affect program
    A in any way that program A does not allow. This gives you a lot
    more control, with the price you pay being that you need to open
    some kind of communications channel between the two programs if
    you want more than the simplest kinds of data transfer.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: gmail (figure it out) http://web.torek.net/torek/index.html
     
    Chris Torek, Jun 19, 2011
    #2
    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. darrel
    Replies:
    1
    Views:
    407
    Scott Allen
    Oct 4, 2004
  2. Glennn
    Replies:
    5
    Views:
    6,045
    Jim Higson
    Apr 6, 2006
  3. Replies:
    5
    Views:
    1,131
  4. Replies:
    5
    Views:
    660
  5. Jabba Laci

    import os or import os.path

    Jabba Laci, Sep 6, 2011, in forum: Python
    Replies:
    0
    Views:
    188
    Jabba Laci
    Sep 6, 2011
Loading...

Share This Page