variable to be transmitted from a CGI script to a module

Discussion in 'Python' started by jcj, Sep 18, 2003.

  1. jcj

    jcj Guest

    hello,
    is it possible to have something like :

    script.cgi
    #!/usr/local/bin/python
    # -*-python-*-
    global MY_VARIABLE
    MY_VARIABLE = "myvalue"
    import module1
    module1.my_function()

    module1.py
    # -*-python-*-
    global MY_VARIABLE
    if MY_VARIABLE:
    print 'variable defined'
    else:
    print 'variable NOT defined'
    def my_function():
    print 'my_function'

    when I try to compile the module, I obtain this message :
    Traceback (most recent call last):
    File "<string>", line 1, in ?
    File "module1.py", line 3, in ?
    if MY_VARIABLE:
    NameError: global name 'MY_VARIABLE' is not defined

    same error when executing the python CGI script

    thanks in advance

    Jean-Claude
    jcj, Sep 18, 2003
    #1
    1. Advertising

  2. jcj

    jcj Guest

    as the goal is to pass a parameter to a module, I found a way using
    the OS environment :

    in the cgi, I do like a putenv :
    os.environ['MY_UNIX_VARIABLE'] = 'myvalue'

    in the module, I do like a getenv :
    MY_PYTHON_VARIABLE = os.environ['MY_UNIX_VARIABLE']
    jcj, Sep 18, 2003
    #2
    1. Advertising

  3. jcj

    M-a-S Guest

    Read also the thread "How to pass parameter to a module?"
    M-a-S

    "jcj" <> wrote in message news:...
    > hello,
    > is it possible to have something like :
    >
    > script.cgi
    > #!/usr/local/bin/python
    > # -*-python-*-
    > global MY_VARIABLE
    > MY_VARIABLE = "myvalue"
    > import module1
    > module1.my_function()
    >
    > module1.py
    > # -*-python-*-
    > global MY_VARIABLE
    > if MY_VARIABLE:
    > print 'variable defined'
    > else:
    > print 'variable NOT defined'
    > def my_function():
    > print 'my_function'
    > Jean-Claude
    M-a-S, Sep 18, 2003
    #3
  4. jcj

    jcj Guest

    "M-a-S" <> wrote in message news:<Qpmab.5255$>...
    > Read also the thread "How to pass parameter to a module?"
    > M-a-S
    >


    you're right, the proper solution is :

    import sys
    print sys.modules['__main__'].myVariable

    thanks a lot
    jcj, Sep 19, 2003
    #4
    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. Replies:
    1
    Views:
    402
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Jun 1, 2006
  2. Raga
    Replies:
    5
    Views:
    588
  3. Grigoris D

    Getting the actual message being transmitted

    Grigoris D, Jul 20, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    99
    Dan Rogers
    Nov 15, 2004
  4. fatted
    Replies:
    1
    Views:
    115
    Steve Grazzini
    Jul 25, 2003
  5. kath
    Replies:
    4
    Views:
    521
    J. Gleixner
    Apr 9, 2007
Loading...

Share This Page