不同 *.py 文件之间的交叉引用可能吗?
Cross referencing between different *.py files possible?
我的程序 tui.py
包含大约 70 个函数,它们按组分类到四个不同的文档中:
tui.py
tbt.py
ens.py
hmm.py
.
大多数函数至少调用一个其他函数,有些函数还调用另一个 *.py
文件中的函数。
我已经尝试将 __init__
函数(如下所示)放入我的 "main" 文档 (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
…
执行上面的代码有什么问题?它如何成为一个工作代码?
编辑: 我收到以下错误,具体取决于更改源文件中的三行:
原始形式的片段
# 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
让 'erase' 被 '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
让 'erase' 被发现
# 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'
尝试更改您的 __init__
方法:
tui.py
import numpy as np
import pandas as pd
import shutil
import os
import tbt
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 (content ['rootpath'], content ['indir']) )
return content ['rootpath'], content ['indir']
def compute (content, hmm) : # here needs to be ens.py, hmm.py, tbt.py included.
content, hmm = tbt.tbt_init (content, hmm)
return content, hmm
def main () :
content = {
'rootpath' : os.path.abspath ( os.getcwd () ),
'indir' : 'a',
'outdir' : 'output',
'datafile' : 'datei.csv',
'configfile' : 'contents.txt',
'savefile' : 'contents.txt',
'model' : np.arange (4),
'datafiles' : [os.path.join (os.getcwd (), 'data.csv'), os.path.join (os.getcwd (), 'data.dat')],
'data' : pd.DataFrame ( np.arange (15).reshape (3, 5) ),
'result' : None,
}
hmm = {
'hmm_a' : np.random.rand (9).reshape (3, 3),
'hmm_b' : np.zeros (3),
'hmm_mu' : np.random.rand (1),
'hmm_pi' : np.random.rand (3),
}
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
if __name__== "__main__":
main()
tbt.py
import os
import tui
def tbt_init (content, hmm) :
# some other stuff
pathname, filename = tui.erase (content) # this lies in tui.py
return content, hmm
我的程序 tui.py
包含大约 70 个函数,它们按组分类到四个不同的文档中:
tui.py
tbt.py
ens.py
hmm.py
.
大多数函数至少调用一个其他函数,有些函数还调用另一个 *.py
文件中的函数。
我已经尝试将 __init__
函数(如下所示)放入我的 "main" 文档 (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
…
执行上面的代码有什么问题?它如何成为一个工作代码?
编辑: 我收到以下错误,具体取决于更改源文件中的三行:
原始形式的片段
# 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
让 'erase' 被 '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
让 'erase' 被发现
# 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'
尝试更改您的 __init__
方法:
tui.py
import numpy as np
import pandas as pd
import shutil
import os
import tbt
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 (content ['rootpath'], content ['indir']) )
return content ['rootpath'], content ['indir']
def compute (content, hmm) : # here needs to be ens.py, hmm.py, tbt.py included.
content, hmm = tbt.tbt_init (content, hmm)
return content, hmm
def main () :
content = {
'rootpath' : os.path.abspath ( os.getcwd () ),
'indir' : 'a',
'outdir' : 'output',
'datafile' : 'datei.csv',
'configfile' : 'contents.txt',
'savefile' : 'contents.txt',
'model' : np.arange (4),
'datafiles' : [os.path.join (os.getcwd (), 'data.csv'), os.path.join (os.getcwd (), 'data.dat')],
'data' : pd.DataFrame ( np.arange (15).reshape (3, 5) ),
'result' : None,
}
hmm = {
'hmm_a' : np.random.rand (9).reshape (3, 3),
'hmm_b' : np.zeros (3),
'hmm_mu' : np.random.rand (1),
'hmm_pi' : np.random.rand (3),
}
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
if __name__== "__main__":
main()
tbt.py
import os
import tui
def tbt_init (content, hmm) :
# some other stuff
pathname, filename = tui.erase (content) # this lies in tui.py
return content, hmm