Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在您自己的模块中的文件之间导入/调用?_Python_Python 2.7_Python 2.6 - Fatal编程技术网

Python 如何在您自己的模块中的文件之间导入/调用?

Python 如何在您自己的模块中的文件之间导入/调用?,python,python-2.7,python-2.6,Python,Python 2.7,Python 2.6,我试图通过创建一个简单的模块来理解如何编写您自己的模块,但我似乎不理解如何使用uu init u_u文件,而整个导入工作都是这样的 现在我有一个名为“helloWorld”的包,其结构如下: helloWorld __init__.py helloWorldFile.py helloBonjourFile.py 以下是每个文件的内容: __init_uuuuuuuuy.py: from helloWorldFile import helloWorldClass hel

我试图通过创建一个简单的模块来理解如何编写您自己的模块,但我似乎不理解如何使用uu init u_u文件,而整个导入工作都是这样的

现在我有一个名为“helloWorld”的包,其结构如下:

helloWorld
    __init__.py
    helloWorldFile.py
    helloBonjourFile.py
以下是每个文件的内容:

__init_uuuuuuuuy.py:

from helloWorldFile import helloWorldClass
helloWorldFile.py:

import helloBonjourFile

class helloWorldClass():
    def __init__(self):
        self.keyword = 'Hello Beautiful World'

    def hello(self):
        print self.keyword
        helloBonjourFile.run()
helloBounjourFile.py:

def run():
    print 'Bonjour Mon Ami!'
因此,我想从“helloWorldFile”运行“helloBonjourFile”中的任何内容,所以我尝试在Python shell中运行它:

import helloWorld
reload(helloWorld)
helloWorld.helloWorldClass().hello()
它很好地打印出了“Hello Beauty World”部分,但之后我不断地得到一个错误:

AttributeError:“模块”对象没有属性“运行”

我很确定我做得不对,如何正确运行“helloWorld”和“helloBonjour”的内容?我想把实际运行这些东西的文件保持在最低限度


如果可能的话,我还想找出一种将参数传递到“helloBonjour”的方法…

我发现,对于其他可能有类似问题的人来说,这是由编辑一个文件并尝试在相同环境下从自定义模块运行引起的,通过对每个文件运行reload()命令解决了这个问题。但您必须按照导入文件的顺序进行操作,在我的情况下,我必须按照以下顺序重新加载:

reload(helloBounjourFile)
reload(helloWorldFile)
reload(helloWorld)

这就应该做到了。。。。如果它没有多次尝试刷新(至少对我来说是这样).

我发现,对于其他可能有类似问题的人来说,这是由于编辑一个文件并尝试在同一环境中从自定义模块运行导致的,通过对每个文件运行reload()命令解决了这一问题。但您必须按照导入文件的顺序进行操作,在我的情况下,我必须按照以下顺序重新加载:

reload(helloBounjourFile)
reload(helloWorldFile)
reload(helloWorld)
这就应该做到了。。。。如果它不多尝试几次来刷新(至少对我来说是这样的)