conditional import into global namespace

Discussion in 'Python' started by mk, Mar 2, 2010.

  1. mk

    mk Guest

    Hello everyone,

    I have a class that is dependent on subprocess functionality. I would
    like to make it self-contained in the sense that it would import
    subprocess if it's not imported yet.

    What is the best way to proceed with this?

    I see a few possibilities:

    1. do a class level import, like:

    class TimeSync(object):

    import subprocess


    2. do an import in init, which is worse bc it's ran every time an
    instance is created:

    def __init__(self, shiftsec, ntpserver):
    import subprocess


    Both of those methods have disadvantage in this context, though: they
    create 'subprocess' namespace in a class or instance, respectively.

    Is there anyway to make it a global import?

    Regards,
    mk
    mk, Mar 2, 2010
    #1
    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. Alec S.
    Replies:
    10
    Views:
    10,146
    Alec S.
    Apr 16, 2005
  2. Jason Heyes
    Replies:
    1
    Views:
    449
    Woebegone
    Nov 19, 2004
  3. Scott Simpson
    Replies:
    2
    Views:
    372
    Peter Otten
    Apr 28, 2006
  4. nate

    import and global namespace

    nate, Jul 11, 2006, in forum: Python
    Replies:
    3
    Views:
    379
    Bruno Desthuilliers
    Jul 12, 2006
  5. Suresh
    Replies:
    3
    Views:
    387
    Suresh
    Jul 29, 2008
Loading...

Share This Page