Open shp with python

2263
3
04-06-2017 02:41 AM
luis_enriquemartin_cañamero
New Contributor

Hello all, I am trying  to create a python´s script to take a shapefile and open it on ArcMap .

This is my code :

--------------------------------------------------------------

import os,string,arcpy
os.system('ArcMap.exe C:\Users\Administrator.DBGS.000\Desktop\shap.mxd')
arcpy.MakeFeatureLayer_management("C:\Users\Administrator.DBGS.000\Desktop\shape\cities.shp")
arcpy.RefreshActiveView()
arcpy.RefreshTOC()

--------------------------------------------------------------

But this code only opens arcmap with shap.mxd and nothing more , however if  I open arcmap and copy the three last lines of my script in arcmap´s python window , it works and open the shape and show the layer.

Why it is working in arcmap´s python window but not with an external python script??

Any idea to open a shapefile automatically with code ??

Thanks a lot in advance¡

Luis.

Tags (2)
0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

because the ability to add the results of a geoprocessing operation to the display (see Geoprocessing options) is either turned off or is not supported by python IDEs external to ArcMap.  Check your options to see if the add results is turned off

0 Kudos
XanderBakker
Esri Esteemed Contributor

You will probably have to add the "layer" (shapefile) to the map: See AddLayer—Help | ArcGIS Desktop scroll down for code samples.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

You can't control an ArcMap session from a stand-alone Python script.  Your first command launches ArcMap, but your second command has absolutely no idea about that ArcMap session or any other ArcMap session that is running.

If you want to open an MXD with a certain layer added to it, you need to use ArcPy Mapping to edit the MXD first and then launch ArcMap after saving the changes to the MXD.