Handling import errors

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

  1. What is the pythonic way to handle imports error? What is bugging me
    is that the imports can't be inside a function (because I use them in
    different places in the script and thus they have to be in the global
    scope). I would write something like:

    try:
     
    Guillaume Martel-Genest, Jun 21, 2011
    #1
    1. Advertising

  2. Guillaume Martel-Genest

    Tim Johnson Guest

    * Guillaume Martel-Genest <> [110621 12:53]:
    > What is the pythonic way to handle imports error? What is bugging me
    > is that the imports can't be inside a function (because I use them in
    > different places in the script and thus they have to be in the global
    > scope). I would write something like:

    Suppose you do something like this:
    try :
    mod = __import__('mymodulename')
    except ImportError:
    pass ## replace with error handling here

    so `mod' is global if you execute the above code as top-level,
    *but* you can also pass `mod' as an argument like any other
    variable, as far as I know and far as I have done.

    --
    Tim
    tim at johnsons-web dot com or akwebsoft dot com
    http://www.akwebsoft.com
     
    Tim Johnson, Jun 21, 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. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    955
    Mark Goldin
    Jan 17, 2004
  2. Paul Opal
    Replies:
    12
    Views:
    954
    Paul Opal
    Oct 11, 2004
  3. plb
    Replies:
    0
    Views:
    396
  4. plb
    Replies:
    0
    Views:
    316
  5. Guillaume Martel-Genest

    Handling import errors

    Guillaume Martel-Genest, Jun 21, 2011, in forum: Python
    Replies:
    3
    Views:
    379
    Guillaume Martel-Genest
    Jun 23, 2011
Loading...

Share This Page