[Tutor] Use of a global variable in many scripts

Aldo Duran aduran@us.ibm.com
Fri, 22 Mar 2002 07:58:23 -0600


This is a multipart message in MIME format.
--=_alternative 004CC28D86256B84_=
Content-Type: text/plain; charset="us-ascii"

Hello everyone

I am new to python and I have some questions, here is my first.

I have a set of scripts, and I would like to use a variable being set in
the main program and use in all the others scripts. I have a file1.py
which defines global variabel var1 and then I import file2.py, I tryed
to use the variable from file2.py, but I get the exception

Traceback (most recent call last):
  File "/tivoli/homes/4/aduran/bin/py/file1.py", line 7, in ?
    file2.useVar1()
  File "/tivoli/homes/4/aduran/bin/py/file2.py", line 6, in useVar1
    print var1
NameError: global name 'var1' is not defined

How can I use the same global variables in all my scripts?

I know that using global variables is not a very good practice but
sometimes it is easy, convenient, quick and simple.

Thanks
aldo

file1.py
-------------------------------------------------
#!/usr/bin/env python2.2

global var1
var1 = 1
import file2

file2.useVar1()

file2.py
-------------------------------------------------
#!/usr/bin/env python2.2

global var1

def useVar1():
    print "var1 value == " ,var1

file3.py
-------------------------------------------------
#!/usr/bin/env python2.2

global var1

def useVar1():
    print "another file using the same variable == " ,var1

--=_alternative 004CC28D86256B84_=
Content-Type: text/html; charset="us-ascii"


<br><font size=2 face="sans-serif">Hello everyone</font>
<br>
<br><font size=2 face="sans-serif">I am new to python and I have some questions, here is my first.</font>
<br>
<br><font size=2 face="sans-serif">I have a set of scripts, and I would like to use a variable being set in</font>
<br><font size=2 face="sans-serif">the main program and use in all the others scripts. I have a file1.py</font>
<br><font size=2 face="sans-serif">which defines global variabel var1 and then I import file2.py, I tryed</font>
<br><font size=2 face="sans-serif">to use the variable from file2.py, but I get the exception</font>
<br>
<br><font size=2 face="sans-serif">Traceback (most recent call last):</font>
<br><font size=2 face="sans-serif">&nbsp; File &quot;/tivoli/homes/4/aduran/bin/py/file1.py&quot;, line 7, in ?</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; file2.useVar1()</font>
<br><font size=2 face="sans-serif">&nbsp; File &quot;/tivoli/homes/4/aduran/bin/py/file2.py&quot;, line 6, in useVar1</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; print var1</font>
<br><font size=2 face="sans-serif">NameError: global name 'var1' is not defined</font>
<br>
<br><font size=2 face="sans-serif">How can I use the same global variables in all my scripts?</font>
<br>
<br><font size=2 face="sans-serif">I know that using global variables is not a very good practice but</font>
<br><font size=2 face="sans-serif">sometimes it is easy, convenient, quick and simple.</font>
<br>
<br><font size=2 face="sans-serif">Thanks</font>
<br><font size=2 face="sans-serif">aldo</font>
<br>
<br><font size=2 face="sans-serif">file1.py</font>
<br><font size=2 face="sans-serif">-------------------------------------------------</font>
<br><font size=2 face="sans-serif">#!/usr/bin/env python2.2</font>
<br>
<br><font size=2 face="sans-serif">global var1</font>
<br><font size=2 face="sans-serif">var1 = 1</font>
<br><font size=2 face="sans-serif">import file2</font>
<br>
<br><font size=2 face="sans-serif">file2.useVar1()</font>
<br>
<br><font size=2 face="sans-serif">file2.py</font>
<br><font size=2 face="sans-serif">-------------------------------------------------</font>
<br><font size=2 face="sans-serif">#!/usr/bin/env python2.2</font>
<br>
<br><font size=2 face="sans-serif">global var1</font>
<br>
<br><font size=2 face="sans-serif">def useVar1():</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; print &quot;var1 value == &quot; ,var1</font>
<br>
<br><font size=2 face="sans-serif">file3.py</font>
<br><font size=2 face="sans-serif">-------------------------------------------------</font>
<br><font size=2 face="sans-serif">#!/usr/bin/env python2.2</font>
<br>
<br><font size=2 face="sans-serif">global var1</font>
<br>
<br><font size=2 face="sans-serif">def useVar1():</font>
<br><font size=2 face="sans-serif">&nbsp; &nbsp; print &quot;another file using the same variable == &quot; ,var1</font>
<br>
--=_alternative 004CC28D86256B84_=--