1 year ago
#386619
dominikdh
pyinstaller + pythonnet(clr): module not found after adding dll as reference
I'm trying to pack a some code from me into an exe with help of pyinstaller. In the code I use some proprietary .net third party libraries which are included with the pythonnet/clr module. If I run the script everything works fine, but after using pyinstaller to create the package I'm getting a ModuleNotFoundError.
Beside adding "clr" as hidden import, I add the used dll's to the datas argument to copy them from the original place to the (pyinstaller) working directory:
import os
# used_dlls: filename list of used dll's
used_dlls = ['X.Y1.Z1', 'X.Y1.Z2', 'X.Y2.Z3']
# dll_path: dict with paths
dll_path = {
'not_frozen': r'C:\Program Files\...', # source path of the directory with the dll's
'frozen': r'FolderWithinProject' # target path for the dll's in the working dir
}
datas = []
for dll in used_dlls:
data.append(
(os.path.join(dll_path['not_frozen'], dll+'.dll'), dll_path['frozen'])
)
datas.append((r'C:\Users\...\.conda\envs\...\Lib\site-packages\Python.Runtime.dll', '.'))
a = Analysis(['main.py'],
pathex=[],
binaries=[],
datas=datas,
hiddenimports=['clr'],
...
In my program I am using the source or the copied dll's depending if it is frozen by pyinstaller or not:
import sys
import clr
if getattr(sys, 'frozen', False):
path = dll_path['frozen']
else:
path = dll_path['not_frozen']
sys.path.insert(0, path)
for dll in used_dlls:
clr.AddReference(dll)
import X.Y1.Z1 as z1
from X.Y1.Z2 import a1, a2
from X.Y2.Z3 import a3
But running the .exe ends in:
ModuleNotFoundError: No module named 'X'
Coping the dll's seems to work, after running the pyinstaller the dll's are as expected in the dll_path['frozen'] folder. Also adding them as reference clr.AddReference(dll) might work, at least I get no error.
What else do I have to consider to make the import of the dll's working?
python
pyinstaller
python.net
0 Answers
Your Answer