我在Python中有以下代码。这是更大的代码库的一部分。 它曾经工作过,但最近开始抱怨。NameError:全局名称'deployment_mode'未定义
我打电话test.py
这样的:
python -c "import test; test._register_cassandra_service(True)"
这是遗留代码,并在过去行之有效。这看起来很奇怪我为
print('jjj')
从来没有执行,所以deployment_mode
永远不会初始化。
的代码如下:
test.py
import os
def _register_cassandra_service(isReRegister):
print('hhhhhhhhhhhhhhhhhhhh')
print(deployment_mode)
def main():
global deployment_mode
print('jjj')
deployment_mode = os.environ.get('DEPLOY_MODE')
错误
[email protected]:/opt/cisco/vms-installer/scripts$ python -c "import test; test._register_cassandra_service(True)"
hhhhhhhhhhhhhhhhhhhh
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "test.py", line 5, in _register_cassandra_service
print(deployment_mode)
NameError: global name 'deployment_mode' is not defined
任何帮助,将不胜感激。
'main()'不会自动运行。如果你需要它运行,你需要调用它。 – khelwood
@khelwood任何想法如何工作更早 –
@Chris_vr:你明确调用'main()'或没有把它放在函数中? 'main()'永远不会自动调用。 –