python 中的值随着上下文的变化而变化
Value Changes in python with Context Changes
我有一个文件夹结构如下的项目:
MainFolder/
__init__.py
Global.py
main.py
Drivers/
__init__.py
a.py
b.py
在Global.py中我这样声明:
#in Global.py file
global_value=''
现在,当我尝试以下脚本时:
#in main.py
import Global
from Drivers import a
Global.global_value=5
a.print_value()
在a.py文件中
from MainFolder import Global
def print_value():
print Global.global_value
输出应该是这样的:
5
但我得到的只是:
''
任何知道此解决方案的人在上下文更改时会发生什么?
在我看来你不应该那样做。要获得某种形式的公共值,请将值写入 file/db,然后从该文件中获取值。
如果这不能满足您的需求,这里是我找到的一些资源,可能会对您有所帮助:
我没有测试过这个,但是这个应该可以用(从 Import a module from a relative path 中获取)
import os, sys, inspect
# realpath() will make your script run, even if you symlink it :)
cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
if cmd_folder not in sys.path:
sys.path.insert(0, cmd_folder)
# use this if you want to include modules from a subfolder
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"subfolder")))
if cmd_subfolder not in sys.path:
sys.path.insert(0, cmd_subfolder)
# Info:
# cmd_folder = os.path.dirname(os.path.abspath(__file__)) # DO NOT USE __file__ !!!
# __file__ fails if script is called in different ways on Windows
# __file__ fails if someone does os.chdir() before
# sys.argv[0] also fails because it doesn't not always contains the path
更多:
- Importing Python modules from different working directory
- Python accessing modules from package that is distributed over different directories
我有一个文件夹结构如下的项目:
MainFolder/
__init__.py
Global.py
main.py
Drivers/
__init__.py
a.py
b.py
在Global.py中我这样声明:
#in Global.py file
global_value=''
现在,当我尝试以下脚本时:
#in main.py
import Global
from Drivers import a
Global.global_value=5
a.print_value()
在a.py文件中
from MainFolder import Global
def print_value():
print Global.global_value
输出应该是这样的:
5
但我得到的只是:
''
任何知道此解决方案的人在上下文更改时会发生什么?
在我看来你不应该那样做。要获得某种形式的公共值,请将值写入 file/db,然后从该文件中获取值。
如果这不能满足您的需求,这里是我找到的一些资源,可能会对您有所帮助:
我没有测试过这个,但是这个应该可以用(从 Import a module from a relative path 中获取)
import os, sys, inspect
# realpath() will make your script run, even if you symlink it :)
cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
if cmd_folder not in sys.path:
sys.path.insert(0, cmd_folder)
# use this if you want to include modules from a subfolder
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"subfolder")))
if cmd_subfolder not in sys.path:
sys.path.insert(0, cmd_subfolder)
# Info:
# cmd_folder = os.path.dirname(os.path.abspath(__file__)) # DO NOT USE __file__ !!!
# __file__ fails if script is called in different ways on Windows
# __file__ fails if someone does os.chdir() before
# sys.argv[0] also fails because it doesn't not always contains the path
更多:
- Importing Python modules from different working directory
- Python accessing modules from package that is distributed over different directories