Any one help me how to declare global variables across the python files
In Below python file i have declared the global variable var1:
File locations for settings.py:
/usr/lib/python2.7/site-packages/project/settings.py
settings.py
global var1
var1 = ""
python file2.py:
Location:
/usr/lib/python2.7/site-packages/file2.py
file2.py
from dev import fullchip
from project import settings
a = 10
b = 20
fullchip.count (a, b)
print var1
script fullchip.py
location
/usr/lib/python2.7/site-packages/dev/fullchip.py
script fullchip.py
from projects import settings
def count(a, b):
global var
var1 = a + b
I am running file2.py
I am expecting var1 to print "30" but i am getting var1 not defined. Basically I want to use var1 as global variables across all the files
Question : I am expecting var1 to print "20" but i am getting var1 not define
Misspelled project
and projects
File locations for settings.py:
/usr/lib/python2.7/site-packages/project/settings.py
but uses
from projects import settings
This works for me, no global
required:
from dev import fullchip from project import settings a = 10 b = 20 fullchip.count (a, b) print(settings.var1)
Output
30
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.