Python only works in ArcMap

6274
19
Jump to solution
02-25-2015 02:41 PM
HeathAnderson
Occasional Contributor II

I was working with Python in IDLE and PyScripter but for some reason can't seem to get the code to work in either.  The code only works in ArcMap.  I am probably missing something simple but can't seem to place my finger on it.  If you can help please let me know.  I know this is simple code, but I want to run this in windows task scheduler.  I really don't want to create a model, run it, then export it as a python script.  I though it would be easier to just create a python script from scratch, perhaps not.

import arcpy
import os

arcpy.env.overwriteOutput = True

arcpy.env.workspace = "O:\\Data\\GIS\\Dept\\Fire\\hydrants"
mxd = os.path.join(arcpy.env.workspace,"FireHydrant_Utility.mxd")
mpk = os.path.join(arcpy.env.workspace,"FireHydrant_Package.mpk")

arcpy.PackageMap_management(mxd,mpk,"CONVERT","CONVERT_ARCSDE","#","ALL","DESKTOP","NOT_REFERENCED","ALL","#","#","#")

print "Map Package Complete"

Cheers,

Heath

19 Replies
PROBERT68
Frequent Contributor

My question IS  how can we know if Pyscripter and ArcMap can communicate to each other BUT what if I want just run  like a debug first before the actully work in ArcMap ?

For example, you write the code and test it and shows it work and then you can run it either in Python Window or Pyscripter to get the results done ?

0 Kudos
robertbowman
New Contributor

Try importing the product license before arcpy to run alone. For example

import arceditor

import arcpy

0 Kudos
HeathAnderson
Occasional Contributor II

The code I test work just fine in ArcMap in the python command window but with i try and run it outside of ArcMap it fails.  More specifically the arcpy.PackageMap_management fails and raises error 9999999.  This is a general error of course.  In thinking that I may have written the code incorrectly or assigned an invalid input I ran the Package Map tool in ArcMap and copied the python code from the geoprocessing results window, then pasted it into PyScripter, and still no luck.

I tried adding importing the arceditor as a module and that had no affect.

I believe that PyScripter can can see the arcpy module because it will callout available classes and methods as you type.

0 Kudos
AdamMarinelli
Occasional Contributor

Hello Heath,

If your script is getting all the way down to line 10 and breaking on the Package Map tool...then your IDE is properly "importing arcpy" and this is likely not the issue.

Can you run a quick test: Create a blank new MXD, add one or two simple layers from a local File Geodatabase, edit the paths in your scripts, and re-run.  See if you get the same error as before.

0 Kudos
AdamMarinelli
Occasional Contributor

Also, your code looks good.  I just ran in on my machine (although, using ArcGIS 10.3) with a different MXD and it completed successfully.

HeathAnderson
Occasional Contributor II

The script seems to be okay in ArcMap.  The code also runs okay in Py.Win.  I ran sys.path in both ArcMap and PyScripter.  The returns were pointing to different folders in Python27.  I think this maybe the problem but not sure, and if it is, I am not sure how to remedy the problem.  I'll attach a screenshot.  Any suggestions???Sys Path compare.png

AdamMarinelli
Occasional Contributor

Did you install the 32-bit or 64-bit version of PyScripter?  From your screenshot it looks like the 64-bit version.  ArcMap is using the 32-bit version of its python install (C:\Python27\ArcGIS10.2) when background geoprocessing is turned off.

This GIS StackExchange question might help.

Also try this:

  1. Double click the batch file found here: "C:\Python27\ArcGIS10.2\Lib\idlelib\idle.bat"
  2. File > Open > "your/package/map/script.py"
  3. Run > Run Module (F5)
HeathAnderson
Occasional Contributor II

I was able to get the script to run in the idle.bat at

  1. "C:\Python27\ArcGIS10.2\Lib\idlelib\idle.bat"

I do have the 64 bit pyscripter installed and am running the 64bit machine.  I believe that i have installed background geoprocessing in ArcMap.  Do i need to call background geoprocessing out in python?  Not really sure what my next steps should be...

0 Kudos
AdamMarinelli
Occasional Contributor

Try the same test but run your script using the .bat file located here:

  • "C:\Python27\ArcGISx6410.2\Lib\idlelib\idle.bat"

Does this complete successfully?  Do you get the 999999 Error when using PyScripter or another method to run your standalone scripts? Try using the 32-bit version of PyScripter.

0 Kudos
HeathAnderson
Occasional Contributor II

I ended up installing PyScripter for 32-bit.