我在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()'永遠不會自動調用。 –