stay ArcGIS In the development Python In the toolbox , introduce ArcPy There is no problem developing some spatial processing tools and logic . however , In more complex scenarios , You need to rely on others Python Module to achieve , For example, reading and writing word、 Inquire about PostgreSQL Database etc. . stay Python The code in the toolbox introduces other Python When the module , Syntax checking , The following mistakes will be reported , The code is as follows :
import arcpy
import psycopg2
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the .pyt file)."""
self.label = "Toolbox"
self.alias = ""
# List of tool classes associated with this toolbox
self.tools = [Tool]
class Tool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Tool"
self.description = ""
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
params = None
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal validation is performed. This method is called whenever a parameter has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool parameter. This method is called after internal validation."""
return
@staticmethod
def replace_word(doc, tag, pv):
# replace in paragraph
for paragraph in doc.paragraphs:
if tag in paragraph.text:
for run in paragraph.runs:
if tag in run.text:
run.text = run.text.replace(tag, pv)
# replace in table
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
for paragraph in cell.paragraphs:
for run in paragraph.runs:
if tag in run.text:
run.text = run.text.replace(tag, pv)
def execute(self, parameters, messages):
"""The source code of the tool."""
return
error :
Traceback (most recent call last):
File "<string>", line 3, in <module>
ImportError: No module named psycopg2
C:\Python27\ArcGIS10.8\Scripts
.cmd
, Then press enter .pip.exe install psycopg2
. Note that pip.exe
, And replace your own module name . If the installation speed is too slow , You can refer to https://hanbo.blog.csdn.net/article/details/106068605, Configure domestic image acceleration