NameError: global name 'deployment_mode' is not defined
<p>I have the following code in Python. This is part of a larger codebase.
It has worked in the past, but recently started complaining.</p>

<p>I am calling <code>test.py</code> like this:</p>

<pre><code>python -c "import test; test._register_cassandra_service(True)"
</code></pre>

<p>It's legacy code and has worked well in the past. It looks odd to me as </p>

<pre><code>print('jjj')
</code></pre>

<p>was never executed, so <code>deployment_mode</code> is never initialized.</p>

<p>The code is as follows:</p>

<p><strong>test.py</strong></p>

<pre><code>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')
</code></pre>

<p><strong>Error</strong></p>

<pre><code>ubuntu@vikdevtest-deploy:/opt/cisco/vms-installer/scripts$ python -c "import test; test._register_cassandra_service(True)"
hhhhhhhhhhhhhhhhhhhh
Traceback (most recent call last):
  File "&lt;string&gt;", line 1, in &lt;module&gt;
  File "test.py", line 5, in _register_cassandra_service
    print(deployment_mode)
NameError: global name 'deployment_mode' is not defined
</code></pre>

<p>Any help would be appreciated.</p>