从子模块调用时,对象无法正常工作

时间:2015-01-15 19:28:15

标签: python python-2.7 import module

你好慷慨的SO'ers,

这是一个有点复杂的问题,但希望与子模块中更全面地使用全局对象有关。

我正在使用一些商业软件,它提供了一个python库,可以通过TCP与他们的应用程序连接。 (我不能发布我们不认为的库的代码。)

我遇到了从子模块调用对象的问题,我认为这通常与全局变量或其他变量有关。基本上,当子模块与所有其他模块(包括创建对象的模块)位于同一目录中时,对象的状态是预期的。
但是,当我将有问题的子模块移动到子文件夹中时,它仍然可以访问该对象,但状态似乎已被更改,并且该对象与商业应用程序的连接不再起作用。

根据this question关于全局变量的一些建议,我已经组织了我的模块文件:

scriptfile.py
pyFIMM/
    __init__.py            # imports all the other files
    __globals.py           # creates the connection object used in most other modules
    __pyfimm.py            # main module functions, such as pyFIMM.connect()
    __Waveguide.py         # there are many of these files with various classes and functions
    (...other files...)
    PhotonDesignLib/
        __init__.py        # imports all files in this folder
        pdPythonLib.py     # the commercial library
    proprietary/
        __init__.py        # imports all files in this folder
        University.py      # <-- The offending child-module with issues

pyFIMM/__init__.py像这样导入子文件:

from __globals import *         # import global vars & create FimmWave connection object `fimm`
from __pyfimm import *          # import the main module
from __Waveguide import *.
(...import the other files...)
from proprietary import *      # imports the subfolder containing `University.py`

子文件夹“ PhotonDesignLib ”中的__init__.py&amp; “专有”都会导致子文件夹中的所有文件都被导入,因此,例如scriptfile.py将访问我的专有文件:import pyFIMM.proprietary.University。这是通过this hint完成的,在proprietary/__init__.py

中编码如下
import os, glob
__all__ = [ os.path.basename(f)[:-3] for f in glob.glob(os.path.dirname(__file__)+"/*.py")]

(来自少数不同机构的众多编码人员将拥有自己的专有代码,因此我们可以共享基本代码,但这样就可以保留我们的专有文件/功能,而无需更改任何基本代码/导入语句。我现在意识到,对于更静态的PhotonDesignLib文件夹,这是过度的。)

文件__globals.py创建了我需要用来与他们的商业应用程序通信的对象,使用此代码(这是此文件中的所有代码):

import PhotonDesignLib.pdPythonLib as pd    # the commercial lib/object
global fimm
fimm = pd.pdApp()      # <- - this is the offending global object

我的所有子模块都包含from __global import *语句,并且能够访问对象fimm,而无需将其明确声明为global var,而没有任何问题。

所以我运行scriptfile.py,它有一个像from pyFIMM import *这样的导入语句。 最重要的是,scriptfile.py在发出任何需要通信的命令之前,通过顶部的fimm.connect()启动与应用程序建立的TCP连接,并且所有其他模块在各种例程中调用fimm.Exec(<commands for app>)一直运用良好 - fimm对象到目前为止所有模块都可以访问,并保持连接状态没有问题。

我遇到的问题是文件proprietary/University.py只能在fimm对象置于pyFIMM根级目录(即与{相同的文件夹)时成功使用该对象。 {1}}等。)。但是当从__globals.py子文件夹中导入University.py时,它会给我一个“应用程序未初始化”错误当我使用proprietary对象时,好像该对象已被覆盖或重新初始化或其他东西。该对象仍然存在,它只是在被该子模块调用时不保持它的连接状态。 (我已经检查过它没有在另一个模块中重新初始化。)

如果脚本在fimm失败后,我使用控制台发送命令,例如。 proprietary/University.py它沟通得很好
我设置pyFimm.fimm.Exec(<command to app>)以在开头打印proprietary/University.py作为测试权限,它工作正常,看起来dir(fimm)对象按预期存在,但后续调用同一文件到{ {1}}表示对象的状态不正确,返回“应用程序未初始化”错误。
这几乎看起来有两个fimm对象 - 一个是主python控制台(和pyFIMM模块)看到的,哪个很好用,另一个fimm.Exec()看到哪个不知道我们叫fimm 1}}已经。同样,如果我将proprietary/University.py放在主模块文件夹“ pyFIMM ”中,它可以正常工作 - fimm.connect()调用按预期运行!

FYI University.py导入fimm.Exec()文件,如下所示:

proprietary/University.py

(仅供参考,在某处,据说__globals.py优于import sys, os, inspect ScriptDir = inspect.currentframe().f_code.co_filename # get path to this module file (ParentDir , tail) = os.path.split(ScriptDir) # split off top-level directory from path (ParentDir , tail) = os.path.split(ParentDir) # split off top-level directory from path sys.path.append(ParentDir) # add ParentDir to the python search path from __globals import * # import global vars & FimmWave connection object global fimm # This line makes no difference, was just trying it. ,因此上面的代码。)

为什么您认为子文件夹中的子模块会导致对象失去状态?

我怀疑问题是我指示inspect导入__file__的方式或我在University.py中使用的“导入此文件夹中的所有文件”方法。但我不知道如何解决它!

感谢您查看此问题,并提前感谢您的深刻见解。

0 个答案:

没有答案