Importing modules inside of modules in Python
I'm trying to create a hierarchy of options. I'm able to creat开发者_Python百科e the first list of options:
- lemur
- gorillas
- chimps
if the user chooses option 1 for lemurs then I run the Lemur.LE() function cause I already imported the lemur module. They are then presented with another set of options:
- Brandy
- Cigars
- Shaving Cream
- Choose a different monkey.
option 4 runs a break which sends them back to the first list. I'm trying to repeat actions from the first architecture so that I can have them choose Shaven.SC() by importing when the function Lemur.LE() is called, but if I place the imports before the function starts then I get a fatal crash at the beginning when I first import lemur, if I call them from within' the LE() function then I get a strange indention exception. Thoughts? Am I making this harder on myself then necessary?
P.S.
Okay here's the Code:
begin = int(raw_input("""Options 1-6"""))
elif begin == 3:
L.Leg()
elif begin == 6:
print "Goodbye"
exit()
Level 2:
def Leg():
begin = int(raw_input("""options 1-5"""))
elif begin == 2:
import LegacyWT
else:
print "Returning to Main Menu."
break
get a strange indention exception. Thoughts? Am I making this harder on myself then necessary?
Indentation exceptions are almost always caused by mixing tabs and spaces in the same file. If you are using a decent editor, you can set it to automatically convert tabs to spaces. If you are not using a decent, stop it, and use a decent editor.
It's not clear what you are doing wrong without a code sample. In general import is a statement like any other and can be used in any scope. So:
def LE():
...
if option == 'Shaving Cream':
import Shaven
Shaven.SC()
elif option == ...
should work just fine.
精彩评论