This question already has an answer here:
I have spent hours attempting to find the solution to the following problem online, but all answers I have found are quite old, and none work. Here is the situation: I have a variety of .shp files. I have a blank mxd file. I want to add the .shp files into the .mxd file.
Various stackoverflow and stackexchange answers would have me believe that the answer to my problem lies in either: arcpy.MakeFeatureLayer_management(,) or arcpy.mapping.Layer()
Unfortunately, I get a similar error no matter what approach I take.
The exact code from my most recent attempt (I'm really not sure how this could possibly be simpler):
import arcpy arcpy.env.workspace = "C:/test" arcpy.Exists("shapefile.shp") #returns True arcpy.mapping.Layer("shapefile.shp")
It is at this point that I receive the following error: Traceback (most recent call last): ... File "C:\Program Files\ArcGIS\Server\arcpy\arcpy\arcobjects\mixins.py", line 289 in init super(LayerMixin, self).init(lyrfile) File "C:\Program Files\ArcGIS\Server\arcpy\arcpy\arcobjects_base.py", line 47, in init for arg in args ValueError: Object: CreateObject Layer invalid data source
Edit: exact code from my attempt using the other function
import arcpy arcpy.env.workspace = "C:/test" arcpy.Exists("shapefile.shp") #returns True arcpy.MakeFeatureLayer_management("shapefile.shp","myshapelayer")
In this case, I receive the following error: Traceback (most recent call last): ... arcgisscripting.ExecuteError: ERROR 000229: Cannot open shapefile.shp Failed to execute .
Can anyone advise me as to how to proceed? I have found almost five different solutions suggesting that this is the procedure which I should follow, but it definitely isn't working for me. I am using Python 2.7.
Additional Notes: -I have manually loaded the shapefile in ArcMap, it loads and displays correctly.