我有一个文件夹结构如下的项目:
MainFolder/
__init__.py
Global.py
main.py
Drivers/
__init__.py
a.py
b.py
在全局.py我宣布如下:
#in Global.py file
global_value=''
现在当我尝试下面的脚本时:
#in main.py
import Global
from Drivers import a
Global.global_value=5
a.print_value()
在.py文件中
from MainFolder import Global
def print_value():
print Global.global_value
输出应该是这样的:
5
但我得到的只是:
''
有没有人用这个解决方案当上下文改变时会发生什么??你知道吗
我认为你不应该那样做。要获得某种形式的公共值,请将该值写入文件/db,然后从该文件中获取该值。你知道吗
如果这还不能满足您的需要,下面是我找到的一些资源,可能会对您有所帮助:
我没有测试过这个,但是这个应该可以用(从Import a module from a relative path获取)
更多:
相关问题 更多 >
编程相关推荐