Pyinstaller和Pandas:找不到Python.Runtime

时间:2018-10-05 11:24:25

标签: python pandas

我正在尝试使用pyinstaller从我的模块中构建可执行文件,但是每次尝试都以

结尾
File "C:\Python 3.6.5\lib\site-packages\PyInstaller\hooks\hook-clr.py", line 37, in <module>
raise Exception(pyruntime + ' not found')

异常:找不到Python.Runtime

经过一些研究,我发现这是由于pandas模块引起的。每次我将pandas import插入模块时,都会出现此错误,如果没有pyinstaller会执行此操作。我读到原因可能是PyInstaller正在抓取熊猫python代码,而不是抓取lib。所以我尝试添加

def get_pandas_path():
    import pandas
    pandas_path = pandas.__path__[0]
    return pandas_path


dict_tree = Tree(get_pandas_path(), prefix='pandas', excludes=["*.pyc"])
a.datas += dict_tree
a.binaries = filter(lambda x: 'pandas' not in x[0], a.binaries)

到规格文件并启动

pyinstaller --onefile my_project.spec
无论如何,如[this thread](ImportError with Pyinstaller and Pandas)中所建议的那样,我会遇到相同的错误。

根据要求,我要添加整个Traceback:

Traceback (most recent call last):
  File "C:\Python 3.6.5\Scripts\pyinstaller-script.py", line 11, in <module>
    load_entry_point('PyInstaller==3.4', 'console_scripts', 'pyinstaller')()
  File "c:\python 3.6.5\lib\site-packages\PyInstaller\__main__.py", line 111, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "c:\python 3.6.5\lib\site-packages\PyInstaller\__main__.py", line 63, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "c:\python 3.6.5\lib\site-packages\PyInstaller\building\build_main.py", line 838, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "c:\python 3.6.5\lib\site-packages\PyInstaller\building\build_main.py", line 784, in build
    exec(text, spec_namespace)
  File "<string>", line 17, in <module>
  File "c:\python 3.6.5\lib\site-packages\PyInstaller\building\build_main.py", line 241, in __init__
    self.__postinit__()
  File "c:\python 3.6.5\lib\site-packages\PyInstaller\building\datastruct.py", line 158, in __postinit__
    self.assemble()
  File "c:\python 3.6.5\lib\site-packages\PyInstaller\building\build_main.py", line 500, in assemble
    module_hook.post_graph()
  File "c:\python 3.6.5\lib\site-packages\PyInstaller\building\imphook.py", line 410, in post_graph
    self._load_hook_module()
  File "c:\python 3.6.5\lib\site-packages\PyInstaller\building\imphook.py", line 377, in _load_hook_module
    self.hook_module_name, self.hook_filename)
  File "c:\python 3.6.5\lib\site-packages\PyInstaller\compat.py", line 736, in importlib_load_source
    return mod_loader.load_module()
  File "<frozen importlib._bootstrap_external>", line 399, in _check_name_wrapper
  File "<frozen importlib._bootstrap_external>", line 823, in load_module
  File "<frozen importlib._bootstrap_external>", line 682, in load_module
  File "<frozen importlib._bootstrap>", line 265, in _load_module_shim
  File "<frozen importlib._bootstrap>", line 684, in _load
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "c:\python 3.6.5\lib\site-packages\PyInstaller\hooks\hook-clr.py", line 37, in <module>
    raise Exception(pyruntime + ' not found')
Exception: Python.Runtime not found

Pyinstaller日志:

421 INFO: PyInstaller: 3.4
421 INFO: Python: 3.6.5
421 INFO: Platform: Windows-10-10.0.17134-SP0
421 INFO: wrote C:\Users\LENOVO2\Desktop\GDPRScanner\GDPRScanner+Oracle\core2.spec
421 INFO: UPX is not available.
437 INFO: Extending PYTHONPATH with paths
['C:\\Users\\LENOVO2\\Desktop\\GDPRScanner\\GDPRScanner+Oracle',
 'C:\\Users\\LENOVO2\\Desktop\\GDPRScanner\\GDPRScanner+Oracle']
437 INFO: checking Analysis
437 INFO: Building Analysis because Analysis-00.toc is non existent
437 INFO: Initializing module dependency graph...
437 INFO: Initializing module graph hooks...
437 INFO: Analyzing base_library.zip ...
6201 INFO: running Analysis Analysis-00.toc
6232 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\python 3.6.5\python.exe
6295 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\python 3.6.5\python.exe
6357 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\python 3.6.5\python.exe
6420 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\python 3.6.5\python.exe
6467 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll dependency of c:\python 3.6.5\python.exe
6529 WARNING: lib not found: api-ms-win-crt-locale-l1-1-0.dll dependency of c:\python 3.6.5\python.exe
6592 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\python 3.6.5\VCRUNTIME140.dll
6654 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\python 3.6.5\VCRUNTIME140.dll
6701 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\python 3.6.5\VCRUNTIME140.dll
6764 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of c:\python 3.6.5\VCRUNTIME140.dll
6826 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\python 3.6.5\VCRUNTIME140.dll
6982 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\python 3.6.5\python36.dll
7045 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\python 3.6.5\python36.dll
7107 WARNING: lib not found: api-ms-win-crt-locale-l1-1-0.dll dependency of c:\python 3.6.5\python36.dll
7170 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\python 3.6.5\python36.dll
7248 WARNING: lib not found: api-ms-win-crt-environment-l1-1-0.dll dependency of c:\python 3.6.5\python36.dll
7310 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of c:\python 3.6.5\python36.dll
7373 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\python 3.6.5\python36.dll
7420 WARNING: lib not found: api-ms-win-crt-time-l1-1-0.dll dependency of c:\python 3.6.5\python36.dll
7482 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll dependency of c:\python 3.6.5\python36.dll
7545 WARNING: lib not found: api-ms-win-crt-conio-l1-1-0.dll dependency of c:\python 3.6.5\python36.dll
7607 WARNING: lib not found: api-ms-win-crt-filesystem-l1-1-0.dll dependency of c:\python 3.6.5\python36.dll
7654 WARNING: lib not found: api-ms-win-crt-process-l1-1-0.dll dependency of c:\python 3.6.5\python36.dll
7654 INFO: Caching module hooks...
7670 INFO: Analyzing C:\Users\LENOVO2\Desktop\script\core2.py
8263 INFO: Processing pre-find module path hook   distutils
8763 INFO: Processing pre-safe import module hook   six.moves
12200 INFO: Processing pre-safe import module hook   setuptools.extern.six.moves
13028 INFO: Processing pre-find module path hook   site
13028 INFO: site: retargeting to fake-dir 'c:\\python 3.6.5\\lib\\site-packages\\PyInstaller\\fake-modules'
27415 INFO: Loading module hooks...
27415 INFO: Loading module hook "hook-distutils.py"...
27431 INFO: Loading module hook "hook-encodings.py"...
27540 INFO: Loading module hook "hook-lib2to3.py"...
27556 INFO: Loading module hook "hook-lxml.etree.py"...
27556 INFO: Loading module hook "hook-numpy.core.py"...
27899 INFO: Loading module hook "hook-numpy.py"...
27899 INFO: Loading module hook "hook-openpyxl.py"...
27915 INFO: Loading module hook "hook-pandas.py"...
28805 INFO: Loading module hook "hook-pkg_resources.py"...
29399 INFO: Processing pre-safe import module hook   win32com
29537 INFO: Loading module hook "hook-pydoc.py"...
29552 INFO: Loading module hook "hook-pythoncom.py"...
29927 INFO: Loading module hook "hook-pytz.py"...
30021 INFO: Loading module hook "hook-pywintypes.py"...
30427 INFO: Loading module hook "hook-regex.py"...
30443 INFO: Loading module hook "hook-setuptools.py"...
31162 INFO: Loading module hook "hook-sqlalchemy.py"...
31662 INFO:   Found 4 sqlalchemy hidden imports
31662 WARNING: Hidden import "pysqlite2" not found!
34489 WARNING: Hidden import "sqlalchemy.sql.functions.func" not found!
34489 INFO: Import to be excluded not found: 'sqlalchemy.testing'
34489 INFO: Loading module hook "hook-sqlite3.py"...
34630 INFO: Loading module hook "hook-sysconfig.py"...
34630 INFO: Loading module hook "hook-win32com.py"...
34739 INFO: Loading module hook "hook-xml.etree.cElementTree.py"...
34755 INFO: Loading module hook "hook-xml.py"...
34864 INFO: Loading module hook "hook-_mysql.py"...
34864 INFO: Loading module hook "hook-_tkinter.py"...
34942 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\python 3.6.5\DLLs\_tkinter.pyd
34989 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\python 3.6.5\DLLs\_tkinter.pyd
35067 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\python 3.6.5\DLLs\_tkinter.pyd
35270 INFO: checking Tree
35348 INFO: checking Tree
35364 INFO: Loading module hook "hook-clr.py"...

3 个答案:

答案 0 :(得分:1)

您似乎需要打开hook-clr.py并将Python.Runtime.dll的路径添加到data=[]https://github.com/pyinstaller/pyinstaller/issues/1801

if is_win:
    pyruntime = 'Python.Runtime'
    library = ctypes.util.find_library(pyruntime)
    datas = ['path\\to\\Python.Runtime.dll']

    if library:
        datas = [(library, '')]
    else:
        # find Python.Runtime.dll in pip-installed pythonnet package
        for sitepack in getsitepackages():
            library = join(sitepack, pyruntime + '.dll')
            if exists(library):
                datas = [(library, '')]
        if not datas:
            raise Exception(pyruntime + ' not found')

答案 1 :(得分:1)

对我来说,当我安装pythonnet:pip install pythonnet

时,这个问题就消失了

答案 2 :(得分:0)

我知道一个老问题,但是我无法在网上找到解决方案,包括OP的后续工作,即It_is_Chis的解决方案不能完全起作用。 我发现了相同的初始错误,然后在从It_is_Chris解决后,收到了ValueError的相同问题。但是,在阅读PyInstaller文档(How to use .spec files with PyInstaller)之后,解决方案非常简单:

Relevant snip from PyInstaller webpage

要为像我这样的菜鸟提供更完整的答案,请导航到python site-packages文件夹,然后进入 PyInstaller ,最后进入 hooks (可以在解释器中找到)路径,否则我的目录位于:C:\ Users * user * \ AppData \ Roaming \ Python \ Python38 \ site-packages \ PyInstaller \ hooks。然后打开 hook-clr.py 文件。

然后要做的就是使您对数据的添加变成一个tupple,如下所示:

if is_win:
    pyruntime = 'Python.Runtime'
    library = ctypes.util.find_library(pyruntime)
    datas = [('path\\to\\Python.Runtime.dll','.')]

    if library:
        datas = [(library, '')]
    else:
        # find Python.Runtime.dll in pip-installed pythonnet package
        for sitepack in getsitepackages():
            library = join(sitepack, pyruntime + '.dll')
            if exists(library):
                datas = [(library, '')]
        if not datas:
            raise Exception(pyruntime + ' not found')