导入.dll-s

时间:2017-03-01 10:31:57

标签: python kivy

我有一个Kivy-App,它必须连接到物理设备。 连接和控制设备的代码已经用.dll文件编写。 所以我想从.dll文件中导入一些函数。

我设法在python中导入它。这是代码:

mydll.py:

import clr

# import dll-s
try:
    clr.AddReference("CETAx10_x15")
    clr.AddReference("Converter")
    clr.AddReference("NLog")
    clr.AddReference("ValueDefine")
    clr.AddReference("Python.Runtime")
except:
    clr.AddReference("cls\\CETAx10_x15")
    clr.AddReference("cls\\Converter")
    clr.AddReference("cls\\NLog")
    clr.AddReference("cls\\ValueDefine")
    clr.AddReference("cls\\Python.Runtime")

import CETAx10_x15
import Converter
import NLog
import ValueDefine
import Python.Runtime
import System


def get_openports():

    temp = []
    output = []

    ports_reply = CETAx10_x15.CETA.get_ports(temp)

    for elem in ports_reply[1]:
        output.append(elem)

    return output

print(get_openports())

运行它会返回[' COM3',' COM4'],所以它会做到应有的。

我的Kivy App运行完美无需导入此mydll.py文件。 在我的Kivy应用程序中导入此mydll.py将返回以下错误消息:

[INFO   ] [Logger      ] Record log in C:\Mirk\CETACal\logs\kivy_17-03-01_150.txt
[INFO   ] [Factory     ] 193 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Kivy        ] v1.9.2.dev0, git-c6018cc, 20170222
[INFO   ] [Python      ] v3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)]
[INFO   ] [Logger      ] Record log in C:\Mirk\CETACal\logs\kivy_17-03-01_151.txt
[INFO   ] [OSC         ] using <thread> for socket
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] GLEW initialization succeeded
[INFO   ] [GL          ] Backend used <glew>
[INFO   ] [GL          ] OpenGL version <b'4.3.0 - Build 10.18.14.4432'>
[INFO   ] [GL          ] OpenGL vendor <b'Intel'>
[INFO   ] [GL          ] OpenGL renderer <b'Intel(R) HD Graphics 4600'>
[INFO   ] [GL          ] OpenGL parsed version: 4, 3
[INFO   ] [GL          ] Shading version <b'4.30 - Build 10.18.14.4432'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <32>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [EZS-2G      ] The system parameters are: ['C:\\Mirk\\CETACal\\main.py']
['COM3', 'COM4']
 Traceback (most recent call last):
   File "C:\Mirk\CETACal\main.py", line 2708, in <module>
     EZS.run()
   File "C:\Python34\lib\site-packages\kivy\app.py", line 802, in run
     root = self.build()
   File "C:\Mirk\CETACal\main.py", line 1745, in build
     self.root = Builder.load_file(os.path.join(cls.config.kvPATH, 'root.kv'))
   File "C:\Python34\lib\site-packages\kivy\lang\builder.py", line 301, in load_file
     return self.load_string(data, **kwargs)
   File "C:\Python34\lib\site-packages\kivy\lang\builder.py", line 380, in load_string
     self._apply_rule(widget, parser.root, parser.root)
   File "C:\Python34\lib\site-packages\kivy\lang\builder.py", line 500, in _apply_rule
     rule.canvas_before, rootrule)
   File "C:\Python34\lib\site-packages\kivy\lang\builder.py", line 804, in _build_canvas
     Instruction = Factory.get('Instruction')
   File "C:\Python34\lib\site-packages\kivy\factory.py", line 139, in __getattr__
     module = __import__(name=item['module'], fromlist='.')
 TypeError: __import__() takes at least 1 argument (0 given)

如您所见,[&#39; COM3&#39;,&#39; COM4&#39;]已打印出来。

我的应用和&#34; mydll.py&#34;奇迹般有效。 但导入后它将失败。

我只是不明白为什么两个独立工作的程序在导入后失败。

我正在使用: Win7的 python 3.4 Kivy 1.9.2.devs0(发布时间:22.02)

1 个答案:

答案 0 :(得分:0)

管理解决它。我添加了以下行:

来自importlib的

导入导入

到文件:

C:\ Python34 \ LIB \站点包\ kivy \ factory.py