模块中的 Python 类未在一台计算机上加载,但在另一台计算机上加载
所以我有两个文件: 文件 1 有这个方法:
import MyGlobals
global old_function
def init():
import ModuleB
global old_function
MyGlobals.SomeNumber = 0
old_function = ModuleB.someClass.function
ModuleB.someClass.function = someNewFunction
文件 2 有一个类“someClass”和一个类“someOtherClass”。话虽这么说。 当我在计算机上运行代码时,它运行良好并且达到了我的预期。当我在我朋友的计算机上运行此代码时,它是相同的 Windows 7 版本,具有相同的 python 版本 2.5.4,并且具有相同的代码(两者都在拇指驱动器上),它收到错误“模块不包含 someClass”
我希望这对我想说的有意义。这是与工作相关的,因此代码片段不会被大声说出。这让我非常困惑为什么会出现这种情况。我什至尝试“from ModuleB import someClass”来查看 someClass 是否有效,但它仍然说 someClass 不在 moduleB 中,而 someCLass 肯定在 moduleB 中......
任何想法将不胜感激!
So I have two files:
File 1 has this method in it:
import MyGlobals
global old_function
def init():
import ModuleB
global old_function
MyGlobals.SomeNumber = 0
old_function = ModuleB.someClass.function
ModuleB.someClass.function = someNewFunction
File 2 has a class "someClass" and a class "someOtherClass". That being said.
When I run my code on my computer it works great and it does what I expect it to. When I run this code on my friends computer which is the same build of windows 7 with the same python version 2.5.4, and with the same code(on a thumb drive for both) it gets an error "Module does not contain someClass"
I hope this makes sense in what I am trying to say. It is work related therefore code snippets aren't aloud. This one has me extremely stumped on why this would be the case. I even tried "from ModuleB import someClass" to see if someClass would work, but it still said that someClass is not in moduleB, while someCLass is definitely in moduleB...
Any ideas would greatly be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,很明显您正在使用不同版本的
ModuleB
。我大胆猜测,即使您从拇指驱动器运行代码,您也已将ModuleB.py
放在 PYTHONPATH 中的其他位置,并且它在您的计算机上运行该版本,但不在您的计算机上运行。朋友们。这很容易检查:我敢打赌这不会打印出您所期望的内容!
另一方面,您不需要代码片段中的第一个
global
声明——它已经是全局范围了。Well, it's fairly clear that you are using different versions of
ModuleB
. I would hazard a guess that even though you are running the code from a thumb drive, you have putModuleB.py
somewhere else in your PYTHONPATH and it is running that version on your computer, but not on your friend's. This is easy to check:I'll bet that doesn't print what you're expecting!
On a different note, you don't need the first
global
declaration in your code snippet -- that's already the global scope.