我的程序tui.py
由大约 70 个函数组成,它们按组分类到四个不同的文档中:
tui.py
tbt.py
ens.py
hmm.py
.
大多数函数至少调用一个其他函数,有些还调用位于另一个*.py
文件中的函数。
我已经尝试在我的__init__
“主”文档(tui.py
tui.__init__
tui.py
(部分是为了让它尽可能简单)
import shutil
import os
from tbt import *
from ens import *
from hmm import *
def window () :
print ( '\n//======================================================\\\\' )
print ( '||\t\t\t\t\t\t\t||' )
print ( '||\t\t\t\t\t\t\t||' )
print ( '||\tR E ad-in of saved variables from file(s) E\t||' )
print ( '||\tDe L etion of saved files or created dirs L\t||' )
print ( '||\tStart of computation . . . . . . . . . . B\t||' )
print ( '||\tShow this window . . . . . . . . . . . . W\t||' )
print ( '||\tExit program . . . . . . . . . . . . . . Q\t||' )
print ( '||\t\t\t\t\t\t\t||' )
print ( '||\t\t\t\t\t\t\t||' )
print ( '\\\\======================================================//\n' )
def erase (content) : # simplified version, to keep the code comprehensible
shutil.rmtree ( os.path.join (pathname, filename) )
return pathname, filename
def compute (content, hmm) : # here needs to be ens.py, hmm.py, tbt.py included.
content, hmm = tbt_init (content, hmm)
return content, hmm
def __init__ (self, content, hmm) :
window ()
give_in = input ( '\tWhat comes next? ' )
while give_in != 'q' and give_in != 'Q' :
if len (give_in) > 1 : # check wether exactly one command was put in.
give_in = input ( 'Type and execute the commands one by one. ' )
elif give_in == 'N' :
give_in = input ( '\tAnd now? [Window with W.] ' )
elif give_in == 'e' or give_in == 'E' :
content, hmm = read_file (content, hmm)
give_in = 'N'
elif give_in == 'l' or give_in == 'L' :
pathname, filename = erase (content)
give_in = 'N'
elif give_in == 'b' or give_in == 'B' :
content, hmm = compute (content, hmm)
give_in = 'N'
elif give_in == 'w' or give_in == 'W' :
window (verbose)
give_in = 'N'
else :
give_in = input ( 'Unknown command. Exit program with Q. ' )
if give_in == 'q' or give_in == 'Q' : # The if statement isn't really needed indeed, but it's there, just in case.
quit ()
quit () # This also isn't really needed indeed, but it's there, just in case.
return content, hmm
tbt.py
def tbt_init (content, hmm) :
# some other stuff
os.mkdir (content ['path'])
pathname, filename = erase (content) # this lies in tui.py
return content, hmm
这个版本没有给我任何输出。
将所有四个文件都导入到其他每个文件中也无济于事。我还需要在每个子文件中导入python
模块(os
, shutil
),尽管它们已经被导入tui.py
...</p>
执行上面的代码有什么问题?它将如何成为工作代码?
编辑:我收到以下错误,具体取决于更改源文件中的三行:
原始形式的片段
# tui.py
from tbt import *
content, hmm = tbt_init (content, hmm)
# tbt.py
## nothing imported here
pathname, filename = erase (content)
File "tui.py", line 62, in <module>
content, hmm = compute (content, hmm)
File "tui.py", line 44, in compute
content, hmm = tbt_init (content, hmm)
File /home/user/tbt.py, line 5, in tbt_init
pathname, filename = erase (content) # this lies in tui.py
NameError: name 'erase' is not defined
让“tbt.py”找到“擦除”
# tui.py
from tbt import *
content, hmm = tbt_init (content, hmm)
# tbt.py
from tui import *
pathname, filename = erase (content)
File "tui.py", line 5, in <module>
from tbt import *
File "/home/user/tbt.py", line 2, in <module>
from tui import *
File "/home/user/tui.py", line 62, in <module>
content, hmm = compute (content, hmm)
File "/home/user/tui.py", line 44, in compute
content, hmm = tbt_init (content, hmm)
NameError: name 'tbt_init' is not defined
制作绝对模块名称
# tui.py
import tbt
content, hmm = tbt.tbt_init (content, hmm)
# tbt.py
## nothing imported
pathname, filename = erase (content)
File "tui.py", line 63, in <module>
content, hmm = compute (content, hmm)
File "tui.py", line 45, in compute
content, hmm = tbt.tbt_init (content, hmm)
File "/home/user/tbt.py", line 5, in tbt_init
pathname, filename = erase (content) # this lies in tui.py
NameError: name 'erase' is not defined
让“擦除”被发现
# tui.py
import tbt
content, hmm = tbt.tbt_init (content, hmm)
# tbt.py
import tui
pathname, filename = tui.erase (content)
File "tui.py", line 6, in <module>
import tbt
File "/home/user/tbt.py", line 2, in <module>
import tui
File "/home/user/tui.py", line 63, in <module>
content, hmm = compute (content, hmm)
File "/home/user/tui.py", line 45, in compute
content, hmm = tbt.tbt_init (content, hmm)
AttributeError: module 'tbt' has no attribute 'tbt_init'