0

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\Projects\1911022.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\Projects\1911022.gdb\LEASE_ROAD"
        da.SearchCursor.__init__(self,in_table,field_names)




test = TestArcpy()
  • 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? – Vince Jul 6 at 14:18
  • What version of ArcGIS Desktop have you installed? – PolyGeo Jul 6 at 18:35
  • I am using arcmap 10.7 I have it installed in Program Files, in the typical directories. – Gary Lester Jul 7 at 7:53
  • The Desktop installation doesn't matter, but the Python install is forbidden from having spaces in the name. – Vince Jul 7 at 12:25
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\Projects\1911022.gdb\LEASE_ROAD",["OBJECTID","NAME"]) as sc:
            for row in sc:
                print(row)
        # field_names=["NAME","OBJECTID"]
        # in_table=r"D:\Projects\AutoWellGIS\Projects\1911022.gdb\LEASE_ROAD"
        # da.SearchCursor.__init__(self,in_table,field_names)




test = TestArcpy()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.