Note that there are some explanatory texts on larger screens.

plurals
  1. PONavigate between modules in Python
    primarykey
    data
    text
    <p>I'm trying to navigate between modules in Python but I can't return to main menu. This is the arch: <code>main.py</code> calls <code>opa.py</code> and <code>opb.py</code></p> <p>main.py:</p> <pre><code>import sys, opa, opb def menu(): print '1. Go opA' print '2. Go opB' print '3. Exit' pick = raw_input('Pick one: ') if pick == '1': opa.menu() elif pick == '2': opb.menu() else: sys.exit() menu() </code></pre> <p>opa.py:</p> <pre><code>def menu(): print '1. Speak' print '2. Return' pick = raw_input('Pick one: ') if pick == '1': print 'OpA' elif pick == '2': main.menu() </code></pre> <p>opb.py:</p> <pre><code>def menu(): print '1. Speak' print '2. Return' pick = raw_input('Pick one: ') if pick == '1': print 'OpB' elif pick == '2': main.menu() </code></pre> <p>When I pick 2 in <code>opa.py</code> and <code>opb.py</code> the program breaks with this error [in this case I go onto opa.py and select "2. Return"]:</p> <pre><code>Traceback (most recent call last): File "main.py", line 16, in &lt;module&gt; menu() File "main.py", line 10, in menu opa.menu() File "opa.py", line 8, in menu main.menu() NameError: global name 'main' is not defined </code></pre> <p>If I add <code>from main import menu</code> I get this other one:</p> <pre><code>Traceback (most recent call last): File "main.py", line 1, in &lt;module&gt; import sys, opa, opb File "opa.py", line 1, in &lt;module&gt; from main import menu File "main.py", line 16, in &lt;module&gt; menu() File "main.py", line 10, in menu opa.menu() AttributeError: 'module' object has no attribute 'menu' </code></pre> <p>And I get it before loading opa.py or opb.py</p> <p>Any suggestions?</p> <p><strong>SOLUTION</strong>: The arch was wrong, so I put the imports after the imports. Like that:</p> <pre><code>def menu(): print '1. Go opA' print '2. Go opB' print '3. Exit' pick = raw_input('Pick one: ') if pick == '1': import opa opa.menu() elif pick == '2': import opb opb.menu() else: import sys sys.exit() menu() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload