首页 > 解决方案 > Python:导入从何而来?

问题描述

我在 python 2.7 中的导入遇到了这个奇怪的问题

我的应用程序位于一个目录中,该目录具有一些子目录和更多使用 Pyro 名称服务器同时运行的 python 应用程序以相互通信。

当我运行我的一个应用程序时,它在调用其中一个子方法时在导入时崩溃。

这是一个例外:

Traceback (most recent call last):
  File "ps_logic.py", line 15840, in <module>
    ps_logic = PSLogic(pyro_objects, cfg_handler, status_distributor, voip_processing)
  File "ps_logic.py", line 590, in __init__
    self.smarthopper_initial_check()
  File "ps_logic.py", line 12824, in smarthopper_initial_check
    counters_compared = self.smarthopper_maintenance_action()
  File "ps_logic.py", line 12928, in smarthopper_maintenance_action
    status = self.smart_hopper_logic.status_get()
  File "/home/app_core/flexcore/003-480/ps_logic/smart_devices_logic.py", line 203, in status_get
    return SmartStatusAugmented(self.smart_obj.queue_status_get(), self.smart_obj)
  File "/usr/lib/python2.7/site-packages/Pyro/core.py", line 381, in __call__
    return self.__send(self.__name, args, kwargs)
  File "/usr/lib/python2.7/site-packages/Pyro/core.py", line 456, in _invokePYRO
    return self.adapter.remoteInvocation(name, Pyro.constants.RIF_VarargsAndKeywords, vargs, kargs)
  File "/usr/lib/python2.7/site-packages/Pyro/protocol.py", line 497, in remoteInvocation
    return self._remoteInvocation(method, flags, *args)
  File "/usr/lib/python2.7/site-packages/Pyro/protocol.py", line 536, in _remoteInvocation
    answer = pickle.loads(answer)
ImportError: No module named drivers.smart.smart_common_const

它清楚地表明它无法导入drivers.smart.smart_common_const,但问题是,我的代码中没有那行。

如果我尝试查找该行在哪个文件中(因为我已经在某些文件中修复了它),它什么也没找到:

app_core@003-481 ~/flexcore/003-480 $ grep -R "from drivers.smart.smart_common_const import" .
./drivers/.svn/pristine/23/23e13acbf9e604f179d4625e18b2b992116a98a1.svn-base:from drivers.smart.smart_common_const import *
./drivers/.svn/pristine/65/65655973d3c70a16cc982db59db8f2989366524b.svn-base:from drivers.smart.smart_common_const import *
./drivers/.svn/pristine/3b/3ba2e2518e64db9188b63247b763926544bddd90.svn-base:from drivers.smart.smart_common_const import *
app_core@003-481 ~/flexcore/003-480 $

但是svn文件。

我一直在运行我的 python 应用程序,并-v选择找出它试图从该文件导入的位置。但是在该异常之前它没有返回任何调试行,所以我猜它是以前导入的东西,或者如果导入失败则什么也不显示。

我还删除了所有*.pyc文件并重新启动机器以确保内存中没有留下笔记,但问题仍然存在。

还有其他选择如何找出问题所在吗?我开始绝望了。。

标签: python-2.7python-importpyro

解决方案


PS_Logic(无论它是什么)似乎正在使用 Pyro 对服务器进行远程调用。特别是带有以下内容的行,似乎是一个远程调用:

self.smart_obj.queue_status_get()

服务器发回一个自定义对象,因为它使用 pickle 作为序列化格式,您的客户端程序会尝试重建该对象。显然,您的客户端代码中没有可用的正确模块,因为当它尝试为您导入所需的模块(将响应重新组合成对象)时,泡菜会失败

该 ps_logic 模块的手册中必须有一些内容告诉您如何正确使用它,并且您可能也应该将它安装在客户端中。

(建议不要顺便使用pickle,并坚持使用Pyro的默认序列化程序,但这是另一回事)


推荐阅读