variable to be transmitted from a CGI script to a module

J

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
 
J

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']
 
J

jcj

M-a-S said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top