可以在不同的* .py文件之间进行交叉引用吗?

时间:2019-09-18 10:54:52

标签: python-3.x cross-reference function-reference

我的程序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模块(osshutil),尽管它们已经被导入到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

通过“ 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'

1 个答案:

答案 0 :(得分:1)

尝试更改您的__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
相关问题