python中的导入错误

2024-06-16 14:30:29 发布

您现在位置:Python中文网/ 问答频道 /正文

在file1.py中:

      def test1():
        print "hi"

在file2.py中:

^{pr2}$

输出:

      hi
      hello

现在在文件1中,如果我包含test2,我会得到以下错误:

    from file2 import test2

    def test1():
      print "hi"

   Traceback (most recent call last):
   File "file1.py", line 1, in ?
   from file2 import test2
   File "/root/pyt/file2.py", line 1, in ?
   from file1 import test1
   File "/root/pyt/file1.py", line 1, in ?
   from file2 import test2
  ImportError: cannot import name test2

有人能解释为什么和如何让它工作吗?在


Tags: infrompyimportdeflinerootpyt
2条回答

您尝试访问时的名称doesn't exist in the module。在

这是一个循环导入问题。{{{{cd2>又是从1无法加载,除非导入2,而{}则无法加载,除非导入`1。在

至于如何让它工作,你能解释一下你想做什么吗?为什么不把这两个函数放在同一个模块中,然后一次导入呢?在

相关问题 更多 >