Skip to main content
 首页 » 编程设计

Python之由于导入错误导致 Modelica 连接失败

2025年05月04日8think

我正在尝试使用伯克利模拟实验室提供的 Python27 block 连接 Modelica 和 Python:

http://simulationresearch.lbl.gov/modelica

我使用这个 block 来调用 Python 函数:

    def Test2(WriteValues): 
        ''' Connection Test - Works if started from Dymola 
        ''' 
        #Doing nothing and returning the input 
        ReturnList=WriteValues 
        return (ReturnList) 

完美运行。

现在我需要导入一些模块

    #Importing Python modules works in general 
    import sys 
    import thread 
    import time 

同样有效

现在我只想导入一个不属于 Python 的模块,而是一个站点包:

    def Test1(WriteValues): 
    '''Connection Test - Doesnt work if started from Dymola 
    ''' 
    #Importing some Bacpypes Module 
    #Path is by default C:\Python27\Lib\site-packages\BACpypes-0.7-py2.7.egg\bacpypes 
    #My os is win7 
    from bacpypes.core import run 
    #Doing nothing and returning the input 
    ReturnList=WriteValues 
    return (ReturnList) 

这是行不通的。无论我是在函数内还是全局导入 BACpypes 模块都没有关系——错误总是

    'module' object has no attribute 'argv' 

同事向我指出这可能与多重导入问题有关。 Modelica 每 10 秒调用一次该函数(实时模拟)。

如果我在 Modelica 外部调用函数 Test1,则没有问题。它仅在使用 Python27 block 时失败!

有没有人知道如何使 BACpypes 导入工作?

2013 年 10 月 16 日更新:

我为 Python 目录中的脚本执行和 Modelica 的执行打印了 sys.argv 的值。

Python 目录中的 sys.argv:

    ['C:\\Python27\\Testcon.py'] 

sys.argv 如果从 Modelica 内部调用函数:

    ['modpython'] 

这可能与我收到的错误消息有任何关系吗?

请您参考如下方法:

这个bug是因为bacpypes使用了sys.argv,但是Python解释器没有调用PySys_SetArgv。

这将在下一版本的 Modelica Buildings 库中修复,请参阅 https://github.com/lbl-srg/modelica-buildings/issues/191