1

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.

dingobar
  • 48
  • 6
  • According to the error message you posted it should be `lib`, not `bin` (`os.path.join(sys.prefix, "Library", "lib", "geos_c.dll")` not `os.path.join(sys.prefix, "Library", "bin", "geos_c.dll")`) or do I misunderstand something? – jpeg Nov 16 '18 at 09:22
  • Exactly - thats what I thought. But the dll-file is in fact located in the `bin`-folder, not in the `lib`-folder in my anaconda directory. There is a file in the `lib`-folder called `geos_c.lib`, which may be relevant? – dingobar Nov 16 '18 at 09:38
  • Interesting. It looks like the way Anaconda packages the dependencies is not straightforward... If this is an option for you, you could also make a new Python installation not using Anaconda and install `shapely` there using `pip`, my expectation is that `cx_Freeze` would work correctly in this new installation. Or maybe there is a way to tell `geos` where to find the DLL using an environment variable as for TCL for example, but I don't know that. – jpeg Nov 16 '18 at 09:44
  • It seems to be an issue between Anaconda and `cx_Freeze` affecting several packages, see the post by Henfri at the end of [this cx_Freeze issue](https://bitbucket.org/anthony_tuininga/cx_freeze/issues/43/import-errors-when-using-cx_freeze-with). You could also try [this](https://deparkes.co.uk/2015/02/04/anaconda-whl-install/) with the wheel corresponding to your configuration. – jpeg Nov 16 '18 at 10:01
  • Has there been any progress made on this issue? I am also using an anaconda environment. It seems to be quite difficult to get all of the packages in geopandas to work correctly together, and even more difficult to package them. – skrhee Sep 13 '19 at 22:04
  • @skrhee I eventually gave up and deployed the code as a service in stead (flask API) – dingobar Mar 31 '20 at 09:39

2 Answers2

0

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)])
krflol
  • 1,105
  • 7
  • 12
  • Thanks for the answer. So, in the "packages" entry, I would just include some of the packages that I think the compiler might be missing? – dingobar Nov 15 '18 at 19:03
0

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"))]}
jpeg
  • 2,372
  • 4
  • 18
  • 31