【问题标题】:Python - Modelica connection fails due to import errorPython - 由于导入错误,Modelica 连接失败
【发布时间】:2013-10-15 12:37:57
【问题描述】:

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

http://simulationresearch.lbl.gov/modelica

我使用这个块来调用 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 块时失败!

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

2013 年 10 月 16 日更新:

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

来自 Python 目录的 sys.argv:

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

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

    ['modpython']

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

【问题讨论】:

    标签: python import modelica dymola


    【解决方案1】:

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

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

    【讨论】: