Errors when converting .py to .exe due to arcpy module

1639
7
02-25-2011 01:37 PM
MichaelO_Donnell1
New Contributor II
I am trying to convert a python script to an exe using py2exe. When I do this several errors occur in the STDOUT. For example, I see the following:

The following modules appear to be missing
['_scproxy', 'arcobjects.Result', 'arcobjects._BaseArcObject', 'geoprocessing.env', 'geoprocessing.gp', 'mobile', 'production', 'sa.Raster']

My python script is importing arcpy such as:
# ESRI package
import arcpy
from arcpy import env

Although an exe is created, the executable crashes when it reaches the arcpy import.

Has anyone experienced this (Users or ESRI)?

The reason why I need to do this is because there are many libraries that do not exist on the local machines that will be running this program and it is easiest to 'compile' the program versus installing all the libraries that I require on each local machine.

Thank you for your help,
Mike
Tags (2)
0 Kudos
7 Replies
JasonScheirer
Occasional Contributor III
You will need to figure out another solution (using pip/virtualenv to build a local installation of Python and/or bundling all your extra libraries in a separate folder and adding it to sys.path hint: modulefinder). If your py2exe bundle includes arcpy and/or arcgisscripting you can exclude them in the blacklist in your setup.py -- that should not only fix the majority of the problem but get you around potential license violations of redistributing Esri binaries.
0 Kudos
MichaelO_Donnell1
New Contributor II
Thanks Jason. I am using distributed computing for python applications and we have approx 200 licenses. To work around the problem I described, I run the python script on each machine and use sys.path.append(), which points to an NTFS where the libraries that I require exist (non-standard python libraries). Because each machine has a valid license and installation of Desktop and python, I can run the python script, access arcpy and with the sys.path.append(), I can access the other python libraries I require.

mike
0 Kudos
Juan_FranciscoMartínez_Carmona
New Contributor
I am also trying to convert a python script to an exe using py2exe. The python script is associated with an ArcToolBox, and it works fine as a python file but not as an exe.

I have excluded arcgisscripting module from the setup.py:

from distutils.core import setup
import py2exe
import os

opts = {"py2exe": {"includes":["doc_if_comunes"],
"excludes":['_ssl', "arcgisscripting",
'pyreadline', 'difflib', 'doctest', 'locale',
'optparse', 'pickle', 'calendar'],
"dist_dir":r"W:\0_FUENTES_PROGRAMAS\DOCUMENTACION_INCENDIOS\dist\DOCUMENTACION_INCENDIOS"}}

setup(windows=[os.getcwd() + 'PerimetrosAFrentes.py'],options=opts)


Path to arcgisscripting is set in PerimetroAFrentes.py:

import sys
sys.path.append(r'C:\Archivos de programa\ArcGIS\Bin')

import os
import doc_if_comunes
import arcgisscripting
import string, tempfile

gp = arcgisscripting.create(9.3)
gp.SetProduct("ArcInfo")

def Principal():

gp.AddToolbox(doc_if_comunes.pathArcGIS() + r"ArcToolbox\Toolboxes\Data Management Tools.tbx")

#lineas temporal
tempdir = tempfile.gettempdir()
li_tmp = os.path.join(tempdir,"shp_lines.shp")
if gp.Exists(li_tmp): gp.Delete(li_tmp)

gp.AddWarning(str(gp.Exists(r'ACTUAL\LINEAS')))

desc = gp.Describe(r'ACTUAL\LINEAS')
fc_li_a = desc.CatalogPath



arcgisscripting is properly imported as gp is created, shp_lines deleted and gp.Exists(r'ACTUAL\LINEAS') returns true (LINEAS is a feature layer in ArcMap). Nevertheless, the program breaks down in desc = gp.Describe(r'ACTUAL\LINEAS'). The following Error is written down in a log file:

RuntimeError: ERROR 999999: Error executing function.


PerimetrosAFrentes.py works correctly and gp is properly created, but I don´t figure out why PerimetrosAFrentes.exe colapses in gp.Describe. Any advice will be appreciated.
0 Kudos
JasonScheirer
Occasional Contributor III
Is there a reason you're compiling to an .exe? To obscure the code or because you're bundling third party modules? If you're just trying to obscure the code, you can have two files (in the same directory) like this:

tool_implementation.py

import arcpy
def main():
   <your geoprocessing script>


tool_gp.py

import tool_implementation
tool_implementation.main()


Then set the path of the script tool to tool_gp.py in your toolbox, and run it once. Once tool_implementation.pyc exists, you can delete tool_implementation.py and your implementation will be obscured.
0 Kudos
Juan_FranciscoMartínez_Carmona
New Contributor
Thank you for your answer. The main goal is to obscure the code as you rightly supposed, but I am unable to make it works. I wrote the following script:

obscure.py
import PerimetrosAFrentes
PerimetrosAFrentes.Principal()


but it gives back the same Error:
File "W:\0_FUENTES_PROGRAMAS\DOCUMENTACION_INCENDIOS\DOCUMENTACION_INCENDIOS\PerimetrosAFrentes.py", line 34, in Principal
    fc_li_a = gp.Describe(r"ACTUAL\LINEAS").CatalogPath
RuntimeError: ERROR 999999: Error executing function.
0 Kudos
JasonScheirer
Occasional Contributor III
You may need to either use a full path to your object or set the arcpy.env.workspace value before your Describe:

arcpy.env.workspace = os.path.abspath(os.path.dirname(__file__))
0 Kudos
Juan_FranciscoMartínez_Carmona
New Contributor
Thanks for your answer. I use 'Describe' precisely to get full path to a feature class from a feature layer in ArcMap, so I cannot use full path. Nevertheless, your answer gave me a good cue: the path to the geodatabase can be written in a text file where the Python file is, "os.path.dirname(__file__)". When access the feature class is required, the code reads this text file and builds the full path to the feature class. The problem is over.
0 Kudos