469,304 Members | 1,803 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

variable to be transmitted from a CGI script to a module

jcj
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
Jul 18 '05 #1
3 1489
jcj
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']
Jul 18 '05 #2
Read also the thread "How to pass parameter to a module?"
M-a-S

"jcj" <jc*******@bigfoot.com> wrote in message news:45**************************@posting.google.c om...
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

Jul 18 '05 #3
jcj
"M-a-S" <NO*****@hotmail.com> wrote in message news:<Qp*****************@twister.southeast.rr.com >...
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
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Eyal Lotem | last post: by
reply views Thread by Mike | last post: by
7 posts views Thread by Just Me | last post: by
8 posts views Thread by MakaMaka | last post: by
8 posts views Thread by yinglcs | last post: by
2 posts views Thread by Bruza | last post: by
10 posts views Thread by Mason Barge | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.