本文介绍了Python:为什么导入的模块不能引用另一个导入的模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! main.py: import subone import subtwo subone.py: a ='abc' subtwo.py : print subone.a 运行 python main.py 抛出 NameError:名称'subone'未定义。我希望它打印'abc'。 从 import / code>和课程无效: main.py: 来自subone import *#仅使用来自X import *的示例。 来自subwo import * 打印'来自main.py:',a.out subone.py: A类: out ='def' a = A() subtwo。 py: #这会引发NameError:名称'a'未定义 print a.out #这会引发NameError:名称'A'未定义b = A() print b.out 但将从main.py:def'打印'。 (它也适用于使用 import 。) 为什么这样工作?似乎一旦 subone 被导入,它应该可用于 subtwo 。 这是因为导入的模块相互依赖而不通过父模块是不好的编程?还有其他标准方法吗? 更新: 我现在明白第一个例子将会不行,因为行 print subone.a 无法识别名称 subone ,它不在 subtwo 的命名空间(即使它在 main.py 中),它正在从模块 subtwo 中调用。这可以通过在 subtwo.py 顶部使用 import subone 来修复 - 它不会重新加载模块,但会将其添加到 subtwo 的命名空间,因此 subtwo 可以使用它。 但是这个怎么样: main.py: 来自subone import Nugget 来自subtwo import Wrap wrap = Wrap() print wrap.nugget.gold subone.py: class Nugget: gold ='def' subtwo.py: class Wrap: nugget = Nugget() 我认为,因为 Wrap 和 Nugget 都直接加载到 main 的命名空间中,他们会使用 main 的命名空间并且能够互相引用,但它抛出一个 NameError:name'Nugget'不是d efined 。是因为 Wrap 是在 subwo 的命名空间中被评估/检查的进入 main 的命名空间?解决方案如果你修改了你的子树。以这种方式然后它将工作 import subone print subone.a 当你在subtwo.py中执行subone.a时,你试图访问subtwo.py和命名空间subone中的命名空间subone。 ,应该有一个属性a。 当你这样做 - 在subtwo.py中导入subone时,subone被添加到命名空间,subone命名空间有属性a。所以subone.a会工作。 我还建议您使用dir()来查看如何添加命名空间。 在subtwo.py中,您可以执行以下操作: print dir() import subone print dir() print subone.a 同样,尝试添加import dir()在你的import语句之前和之后,你的想法应该清楚了。 所以在上面的main.py,subone.py和subtwo.py的第一个例子中,main.py中的命名空间将包含'subone'和'subtwo'而subwo。 py将有一个空命名空间,无法访问subone.a。 请考虑以下文件: main.py print在导入subone之前:,dir() import subone print导入subone后在导入subtwo之前:,dir() import subtwo print导入subone和subtwo后:,dir() subone.py a ='abc' subtwo.py print dir() import subone printmodule level print:,subone.a print dir() def printX(): print subone.a 运行main.py的输出: 在导入subone之前:['__ builtin__','__ doc__','__ file __','_ _ 1 __','__ package__'] 导入subone之后和导入subtwo之前:[ '__builtins__','__ doc__','__ file __','_ _ 1 _','_ _ package__','subone'] ['__ builtin ___','__ doc _','__ file __','_ _ 1 _'','_ _ package__'] 模块e级别打印:abc ['__ builtin__','__ doc__','__ file__','_ name__','__ package__','subone'] 导入subone和subtwo之后:['__ builtin__',' __doc__','__ file __','_ name__','__ package__','subone','subtwo'] 一些观察结果 你会注意导入模块subtwo.py,print语句立即执行。 因此,当在main.py中导入subone和subtwo时,main.py的命名空间会被扩充。 这并不意味着命名空间subtwo将被增强。所以a仅在main.py中通过subone.a可用。 当我们在subtwo.py中导入subone时,则subwo的命名空间会增加subone和属性a的模块subone可以通过subone.a获得subow.py main.py:import suboneimport subtwosubone.py:a = 'abc'subtwo.py:print subone.aRunning python main.py throws a NameError: name 'subone' is not defined. I expected it to print 'abc'.Refactoring it to use from import and classes doesn't help:main.py:from subone import * # Only using from X import * for example purposes.from subtwo import *print 'from main.py:', a.outsubone.py:class A: out = 'def'a = A()subtwo.py:# This throws NameError: name 'a' is not definedprint a.out# This throws NameError: name 'A' is not definedb = A()print b.outBUT it will print 'from main.py: def'. (It works when using import too.)Why does it work this way? It seems like once subone is imported, it should be available to subtwo.Is it because it's bad programming to have imported modules depend on each other without going through their 'parent' module? Is there another, standard way to do this?Update:I now understand that the first example will not work because the line print subone.a doesn't recognize the name subone, it not being in subtwo's namespace (even though it's in main.py's), and it is being called from within the module subtwo. This can be fixed by using import subone at the top of subtwo.py -- it will not re-load the module but will add it to subtwo's namespace so subtwo can use it.But what about this:main.py:from subone import Nuggetfrom subtwo import Wrapwrap = Wrap()print wrap.nugget.goldsubone.py:class Nugget: gold = 'def'subtwo.py:class Wrap: nugget = Nugget()I would think that since Wrap and Nugget are both loaded directly into main's namespace, that they would use main's namespace and be able to reference each other, but it throws a NameError: name 'Nugget' is not defined. IS IT because Wrap is evaluated/checked from within subtwo's namespace BEFORE being loaded into main's namespace? 解决方案 If you modified your subtwo.py this way then it will workimport suboneprint subone.aWhen you do subone.a in subtwo.py, you are trying to access the namespace subone in subtwo.py and in the namespace "subone", there should be a attribute "a".When you do - import subone in subtwo.py, then subone is added to the namespace and subone namespace has attribute a. so subone.a will work.I would also suggest that you play with dir() to see how namespaces are being added.In subtwo.py, you can do the following:print dir()import suboneprint dir()print subone.aSimilarly, try adding "print dir()" before and after your import statements and the idea should become clear to you.So in your above first example of main.py, subone.py and subtwo.py, the namespace in main.py will contain 'subone' and 'subtwo' while subtwo.py will have an empty namespace and can not access subone.a.[Edit: Some more explanations]Consider following files:main.pyprint "Before importing subone : ", dir()import suboneprint "After importing subone and before importing subtwo: ", dir()import subtwoprint "After importing subone and subtwo: ", dir()subone.pya = 'abc'subtwo.pyprint dir()import suboneprint "module level print: ", subone.aprint dir()def printX(): print subone.aAnd the output of running main.py:Before importing subone : ['__builtins__', '__doc__', '__file__', '__name__', '__package__']After importing subone and before importing subtwo: ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'subone']['__builtins__', '__doc__', '__file__', '__name__', '__package__']module level print: abc['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'subone']After importing subone and subtwo: ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'subone', 'subtwo']You will notice that importing a module subtwo.py, the print statement is executed immediately.So when subone and subtwo are imported in main.py, the namespace of main.py is augmented.That does not mean that namespace of subtwo will be augmented. so "a" is available only in main.py via subone.aWhen we do import subone in subtwo.py then the namespace of subtwo is augmented with subone and attribute a of module subone is available in subtow.py via subone.a 这篇关于Python:为什么导入的模块不能引用另一个导入的模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-10 11:28