How to import whole namespace into global symbol table? (newbie)

Discussion in 'Python' started by Scott Simpson, Apr 28, 2006.

  1. Suppose I have the following python program:

    def func():
    from sys import stderr, exit
    try:
    f = open("foo", 'r')
    except IOError:
    print >> stderr, "Input file foo does not exist"
    exit(1)

    def main():
    import sys

    if len(args) != 0:
    sys.exit(1)

    func()

    if __name__ == '__main__':
    main()

    Notice that I have two "import sys" statements, one for each function.
    Is there any way to import the "sys" stuff to the global symbol table so
    I don't need two "import" statements?

    Lastly, is there an equivalent of Perl's "die" function? I'm writing to
    stderr and dieing above but I'm not quite sure if this is the "correct" way.
     
    Scott Simpson, Apr 28, 2006
    #1
    1. Advertising

  2. Scott Simpson wrote:
    > def func():
    > from sys import stderr, exit
    > try:
    > f = open("foo", 'r')
    > except IOError:
    > print >> stderr, "Input file foo does not exist"
    > exit(1)


    IOError can be raised when foo exists, e.g. if there's a permission problem.
    It's better to print the actual error message:

    >>> except IOError, e:

    .... print >>stderr, e
    [Errno 13] Permission denied: 'foo'


    > Notice that I have two "import sys" statements, one for each function.
    > Is there any way to import the "sys" stuff to the global symbol table so
    > I don't need two "import" statements?


    move import outside your function scopes.


    > Lastly, is there an equivalent of Perl's "die" function? I'm writing to
    > stderr and dieing above but I'm not quite sure if this is the "correct"
    > way.


    you can reraise the exception and get a stack trace. if you want to
    replicate die "something\n"; (no stack trace), I'm not sure if there's a
    single-function python equivalent.
     
    Edward Elliott, Apr 28, 2006
    #2
    1. Advertising

  3. Scott Simpson

    Peter Otten Guest

    Scott Simpson wrote:

    > Lastly, is there an equivalent of Perl's "die" function? I'm writing to
    > stderr and dieing above but I'm not quite sure if this is the "correct"
    > way.


    You can call sys.exit() with a string. Python will print it to stderr and
    terminate with a nonzero exit status.

    Peter
     
    Peter Otten, Apr 28, 2006
    #3
    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. UJ
    Replies:
    8
    Views:
    61,959
  2. ygao
    Replies:
    2
    Views:
    290
    Alex Martelli
    Jun 18, 2006
  3. nate

    import and global namespace

    nate, Jul 11, 2006, in forum: Python
    Replies:
    3
    Views:
    396
    Bruno Desthuilliers
    Jul 12, 2006
  4. \A_Michigan_User\
    Replies:
    2
    Views:
    940
    \A_Michigan_User\
    Aug 21, 2006
  5. mk
    Replies:
    0
    Views:
    369
Loading...

Share This Page