0

I am using ArcMap 10.7.

I am trying to create a tool that creates dimension lines but I am getting error.

Runtime error 
Traceback (most recent call last):
 File "<string>", line 1, in <module>
 File "<string>", line 218, in main
 File "D:\Projects\AutoWellGIS\Python\DimensionClass.py", line 76, in ReturnFirstPoint
 offsetGeom = Polyline(rArray,sr,False,False)
NameError: global name 'arcpy' is not defined

I am importing arcpy and I also tried to extend the arcpy class by passing it to the class like this.

from arcpy.da import SearchCursor
from arcpy import PointGeometry
from arcpy import Polyline
from arcpy import Point
from arcpy import Array
import math
from math import sqrt
import arcpy
class Dimension(SearchCursor,PointGeometry,Polyline,Point,Array): 
 def __init__(self):
 self.sr = None
 self.insertfields = []
 self.where = None
def ReturnFirstPoint(self,lyr,where):
 sref = self.sr
 fields = ["SHAPE@"]
 with SearchCursor(lyr.name,fields,where_clause=where,spatial_reference=sref) as sc:
 firstPoint = [row[0].firstPoint for row in sc][0]
 return firstPoint

I tried to look at the Python addin but I do not see how they are implementing arcpy in class structure.

I made a test class and try to implement one class from arcgisscripting and I am now getting error:

Traceback (most recent call last):
 File "d:/Projects/AutoWellGIS/Python/testclass.py", line 13, in <module>
 test = TestArcpy()
 File "d:/Projects/AutoWellGIS/Python/testclass.py", line 8, in __init__
 da.SearchCursor.__init__(self,in_table,field_names)
RuntimeError: cannot open 'D:\Projects\AutoWellGIS\Projects1911022円.gdb\LEASE_ROAD'

Here is the test class I create, I am thinking this is not possible, for me, to extend the arcpy classes. It is looking like too much trouble.

from arcgisscripting import da
class TestArcpy(da.SearchCursor):
 def __init__(self):
 field_names=["NAME","OBJECTID"]
 in_table=r"D:\Projects\AutoWellGIS\Projects1911022円.gdb\LEASE_ROAD"
 da.SearchCursor.__init__(self,in_table,field_names)
 
test = TestArcpy()
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jul 6, 2019 at 13:40
2
  • 1
    It you're getting an error on line one, you don't really need to provide any more code than that. ArcPy is only available in Python deployments where it has been installed. Where is your Python installed? Commented Jul 6, 2019 at 14:18
  • 1
    The Desktop installation doesn't matter, but the Python install is forbidden from having spaces in the name. Commented Jul 7, 2019 at 12:25

1 Answer 1

1

I finally had success, here is the code maybe helpful to someone else. I got the code to import the da module from the da.py in the arcpy folder in ArcMap install directory.

import arcpy
__all__=[item for item in dir(arcpy) if not item.startswith('_')]
locals().update(arcpy.__dict__)
class TestArcpy():
 def __init__(self):
 with arcpy.da.SearchCursor(r"D:\Projects\AutoWellGIS\Projects1911022円.gdb\LEASE_ROAD",["OBJECTID","NAME"]) as sc:
 for row in sc:
 print(row)
 # field_names=["NAME","OBJECTID"]
 # in_table=r"D:\Projects\AutoWellGIS\Projects1911022円.gdb\LEASE_ROAD"
 # da.SearchCursor.__init__(self,in_table,field_names)
test = TestArcpy()
Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
answered Jul 8, 2019 at 12:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.