简体   繁体   English

cx_freeze exe 文件在 anaconda 提示符下工作,但在 windows cmd 命令提示符下不起作用?

[英]cx_freeze exe file works in anaconda prompt but not in windows cmd command prompt?

I have spent most of the day trying to compile an exe file from my python script and running it through the vanilla cmd command prompt.我花了一天的大部分时间尝试从我的 python 脚本编译一个 exe 文件并通过 vanilla cmd 命令提示符运行它。 I finally managed to create the exe-file, but weirdly it only runs in the anaconda prompt and not in the cmd.我终于设法创建了 exe 文件,但奇怪的是它只在 anaconda 提示符下运行,而不在 cmd 中运行。

Here is the full error message/traceback:这是完整的错误消息/回溯:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
    module.run()
  File "C:\ProgramData\Anaconda3\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
    exec(code, m.__dict__)
  File "generateKonsekvens.py", line 1, in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\geopandas\__init__.py", line 1, in <module>
    from geopandas.geoseries import GeoSeries
  File "C:\ProgramData\Anaconda3\lib\site-packages\geopandas\geoseries.py", line 7, in <module>
    from shapely.geometry import shape, Point
  File "C:\ProgramData\Anaconda3\lib\site-packages\shapely\geometry\__init__.py", line 4, in <module>
    from .base import CAP_STYLE, JOIN_STYLE
  File "C:\ProgramData\Anaconda3\lib\site-packages\shapely\geometry\base.py", line 17, in <module>
    from shapely.coords import CoordinateSequence
  File "C:\ProgramData\Anaconda3\lib\site-packages\shapely\coords.py", line 8, in <module>
    from shapely.geos import lgeos
  File "C:\ProgramData\Anaconda3\lib\site-packages\shapely\geos.py", line 130, in <module>
    os.path.join(sys.prefix, "Library", "lib", "geos_c.dll"),
  File "C:\ProgramData\Anaconda3\lib\site-packages\shapely\geos.py", line 56, in load_dll
    libname, fallbacks or []))
OSError: Could not find lib geos_c.dll or load any of its variants ['Library\\lib\\geos_c.dll'].

As you can see, it seems to be loking for something in the anaconda folder - which defeats the purpose of freezing the script.如您所见,它似乎在 anaconda 文件夹中寻找某些东西——这违背了冻结脚本的目的。 The geos_c.dll file belongs to fiona/shapely, which are in this case dependencies of the geopandas module. geos_c.dll 文件属于 fiona/shapely,在这种情况下是 geopandas 模块的依赖项。 The geos_c.dll file can be found in the compiled folder (lib/shapely). geos_c.dll 文件可以在编译的文件夹(lib/shapely)中找到。

The script runs just fine in the normal command prompt using该脚本在正常的命令提示符下运行得很好,使用

python generateKonsekvens.py

in the folder.在文件夹中。

What is causing this, and how do I fix it?这是什么原因造成的,我该如何解决?

Python 3.6.3, windows 10 64 bit. Python 3.6.3,Windows 10 64 位。

UPDATE更新

I tried the suggestions of jpeg, and none of them worked (could not find the dll at those locations).我尝试了 jpeg 的建议,但没有一个起作用(在这些位置找不到 dll)。 I tried an adhoc-solution of manually copying the dll to Library/lib/geos_c.dll , which copied some files over, but gives the same error.我尝试了手动将 dll 复制到Library/lib/geos_c.dll的临时解决方案,该解决方案复制了一些文件,但给出了相同的错误。 I then tried with build_exe_options = {'include_files': [(os.path.join(sys.prefix, "Library", "bin", "geos_c.dll"), os.path.join("Library", "bin", "geos_c.dll"))]} , which finds the geos_c.dll file in the anaconda directory.然后我尝试使用build_exe_options = {'include_files': [(os.path.join(sys.prefix, "Library", "bin", "geos_c.dll"), os.path.join("Library", "bin", "geos_c.dll"))]} ,它会在 anaconda 目录中找到geos_c.dll文件。 I also packaged it through the windows cmd this time, and the dlls are included.这次我也是通过windows cmd打包的,dll也包含在内。 The error, however, remains the same... I will now try with a new, fresh conda anaconda venv, but any other ideas are welcome in the meanwhile.但是,错误仍然相同......我现在将尝试使用新的、新鲜的 conda anaconda venv,但同时欢迎任何其他想法。

I suspect you're missing something in your build options.我怀疑您的构建选项中缺少某些内容。 Without knowing the exact package I can't tell you what to include, but an example of the build options would be this (a win32 application for adding virtual printers, hence the win32 stuff)在不知道确切的包的情况下,我无法告诉您要包​​含的内容,但是构建选项的一个示例是这样的(用于添加虚拟打印机的 win32 应用程序,因此是 win32 的东西)

build_exe_options = {"packages": ["os","numpy","idna",'win32com.gen_py',"win32timezone","win32print"],
                     "excludes": ["tkinter"],
                     "includes":[]}

# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
#if sys.platform == "win32":
#    base = "Win32GUI"

setup(  name = "VirtualPrinter",
        version = "0.1",
        description = "KRF AMS VPrint",
        options = {"build_exe": build_exe_options},
        executables = [Executable(r"krfprinter.py", base=base)])

The problem is probably due to the fact that the executable is looking for Library/lib/geos_c.dll (due to the way Anaconda packages shapely ) but the DLLs gets packaged by cx_Freeze into lib/shapely/geos_c.dll (probably as it would be if shapely would have been installed using pip ).问题可能是由于可执行文件正在寻找Library/lib/geos_c.dll (由于 Anaconda 打包的方式shapely ),但 DLL 被cx_Freeze打包成lib/shapely/geos_c.dll (可能就像它会如果使用pip安装shapely话)。 When you run your executable from the Anaconda prompt, the fallback finds the DLL in the Anaconda library path, but if you rum from cmd, this fallback does not work as no copy of the DLL is found in the cmd path.当您从 Anaconda 提示符运行可执行文件时,回退会在 Anaconda 库路径中找到 DLL,但如果您从 cmd 运行,则此回退不起作用,因为在 cmd 路径中找不到 DLL 的副本。

Try to manually include the DLL in the installation directory, the fallback will probably work then.尝试手动将 DLL 包含在安装目录中,然后回退可能会起作用。 You can do this using the build_exe option include_files in your setup script:您可以在安装脚本中使用build_exe选项include_files执行此操作:

import os
import sys
build_exe_options = {'include_files': [os.path.join(sys.prefix, "Library", "lib", "geos_c.dll")]}

...

setup(...
      options = {'build_exe': build_exe_options},
      ...)

If this does not work, try with如果这不起作用,请尝试

build_exe_options = {'include_files': [(os.path.join(sys.prefix, "Library", "lib", "geos_c.dll"), os.path.join("lib", "geos_c.dll"))]}

If this also does not work, try with如果这也不起作用,请尝试

build_exe_options = {'include_files': [(os.path.join(sys.prefix, "Library", "lib", "geos_c.dll"), os.path.join("Library", "lib", "geos_c.dll"))]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM