私は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()'は決して自動的に呼び出されません。 –