【问题标题】:Pyinstaller & pandas: Python.Runtime not foundPyinstaller & pandas:找不到 Python.Runtime
【发布时间】:2020-12-05 06:19:58
【问题描述】:

我正在尝试使用 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 正在抓取 pandas 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"...

【问题讨论】:

  • 我们能看到完整的回溯错误和你使用的python版本吗?
  • 是的,当然,我现在就更新 OP

标签: python pandas


【解决方案1】:

对我来说,安装 pythonnet 后这个问题就消失了:pip install pythonnet

【讨论】:

    【解决方案2】:

    看来您需要打开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')
    

    【讨论】:

    • 嗨,不幸的是,这个解决方案对我不起作用,按照建议修改 hook-clr.py 会返回 ValueError: too many values to unpack (expected 2)。我会用更多信息更新我的 OP
    • @Ignus Ran into this 看起来它也需要一个带有路径和“。”的单个元组的数组。看这里:stackoverflow.com/questions/40676027/…
    【解决方案3】:

    我知道的老问题,但我在网上找不到解决方案,包括 OP 的后续行动,即来自 It_is_Chis 的解决方案没有完全发挥作用。 我发现了相同的初始错误,然后在 It_is_Chris 的解决方案之后,接收 ValueError 时出现了同样的问题。但是,阅读 PyInstaller 文档 (How to use .spec files with PyInstaller) 后,解决方案非常简单:

    Relevant snip from PyInstaller webpage

    为了给像我这样的菜鸟一个更完整的答案,导航到你的 python 站点包文件夹,然后进入 PyInstaller,最后是 hooks(这可以在解释器下找到路径,否则我的路径是:C:\Users*user*\AppData\Roaming\Python\Python38\site-packages\PyInstaller\hooks。然后打开 hook-clr.py 文件。

    然后,只需将您添加到数据中的内容变为元组,如下所示:

    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')
    

    【讨论】:

      猜你喜欢
      • 2021-08-29
      • 2017-02-02
      • 2013-07-13
      • 2014-10-31
      • 1970-01-01
      • 2017-09-12
      • 2022-01-02
      • 2021-06-09
      • 2019-05-16
      相关资源
      最近更新 更多