简体   繁体   English

来自未在python中导入的模块的功能

[英]function from a module not imported in python

Good evenning guys, 晚上好,

I ve got that weird behavior. 我有这种奇怪的行为。 Any help would be appreciated. 任何帮助,将不胜感激。

Here is a function from a module called tankython, calling a function from a module called usual. 这是来自名为tankython的模块的函数,它来自称为“通常”的模块的函数。 However, it looks like Python refuses to recognize the function. 但是,看起来Python拒绝识别该函数。 Here is the code 这是代码

#tankython.py

from usual import * 
from get_data import *
from settings import *
from actif_class import * 

def tanking(list_spreads,settings):    
####    Tanking    ####  
    fenetre = settings.fenetre    
    list_spread_exit,list_spread_temp= [],[]

    ii= 0       
    for spread in list_spreads : 
        avc = ii * 100/float(len(list_spreads))
        print "Be patient. Tanking in progress..." , avc,"%"
        info = tankython (list_spreads,fenetre,ii) 
        list_spread_temp.append(info)
        ii = ii + 1
    list_spread_exit = check_list(list_spread_temp)

    return list_spread_exit

#usual.py

def check_list(list_entry):
    i = 0
    while i < len(list_entry):
        if list_entry[i] == 0 :
            list_entry.pop(i)
        else : 
            i = i+ 1
    return list_entry

Here is the error message which I found really weird as I asked Py to import everything from usual.py: 这是我要求Py从common.py导入所有内容时发现的奇怪消息:

File "tankython.py", line 77, in tanking
  list_spread_exit = check_list(list_spread_temp)

NameError: global name 'check_list' is not defined

One last thing: eventually, if I put the function in tankython module, then Py accepts to goes through the entire process. 最后一件事:最终,如果我将功能放在tankython模块中,那么Py会接受整个过程。 However I really would like to know if there is anything I am doing wrong here. 但是我真的很想知道我在这里做错了什么。

Cheers guys 欢呼的家伙

Try to put file tankython.py and usual.py in the same folder and use the 尝试将tankython.py和通常.py文件放在同一文件夹中,并使用

from usual import check_list

instead of 代替

from usual import *

May help. 可能有帮助。 Or you can try: 或者您可以尝试:

import usual

Then change code like: 然后更改代码,例如:

usual.check_list()

And

from module import *

is not a good way to import something if two packages have the same name functions. 如果两个包具有相同的名称函数,则不是导入某些内容的好方法。

I find another solution, although it is not the most elegan one, especially because it did not explain what went wrong. 我找到了另一种解决方案,尽管它不是最简洁的解决方案,尤其是因为它没有解释出什么问题所在。 However, I renamed the entire module usual by f_usual and it is working perfectly fine. 但是,我用f_usual重命名了整个模块,并且运行正常。

Thanks for your help 谢谢你的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM