问题
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. I finally managed to create the exe-file, but weirdly it only runs in the anaconda prompt and not in the 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. The geos_c.dll file belongs to fiona/shapely, which are in this case dependencies of the geopandas module. The geos_c.dll file can be found in the compiled folder (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.
UPDATE
I tried the suggestions of jpeg, and none of them worked (could not find the dll at those locations). 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. 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. I also packaged it through the windows cmd this time, and the dlls are included. 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.
回答1:
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
). 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.
Try to manually include the DLL in the installation directory, the fallback will probably work then. You can do this using the build_exe
option include_files
in your setup script:
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"))]}
回答2:
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)
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)])
来源:https://stackoverflow.com/questions/53323685/cx-freeze-exe-file-works-in-anaconda-prompt-but-not-in-windows-cmd-command-promp