1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > ArcGIS Python工具箱集成第三方模块的解决办法

ArcGIS Python工具箱集成第三方模块的解决办法

时间:2021-07-10 06:27:59

相关推荐

ArcGIS Python工具箱集成第三方模块的解决办法

在ArcGIS中开发Python工具箱时,引入ArcPy开发一些空间处理工具和逻辑是没有问题。但是,在更复杂的场景下,就需要依赖其他的Python模块来实现,例如读写word、查询PostgreSQL数据库等。在Python工具箱的代码里引入其他Python模块时,语法检查中,会报下面的错误,代码如下:

import arcpyimport psycopg2class 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 toolboxself.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 = Falsedef getParameterInfo(self):"""Define parameter definitions"""params = Nonereturn paramsdef isLicensed(self):"""Set whether tool is licensed to execute."""return Truedef updateParameters(self, parameters):"""Modify the values and properties of parameters before internalvalidation is performed. This method is called whenever a parameterhas been changed."""returndef updateMessages(self, parameters):"""Modify the messages created by internal validation for each toolparameter. This method is called after internal validation."""return@staticmethoddef replace_word(doc, tag, pv):# replace in paragraphfor 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 tablefor 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

错误:

Traceback (most recent call last):File "<string>", line 3, in <module>ImportError: No module named psycopg2

解决方法

找到ArcGIS安装后,自带的python2.7环境下的Scripts目录,比如我的是C:\Python27\ArcGIS10.8\Scripts。在文件地址栏中输入cmd,然后按回车。 在弹出的cmd命令窗口中,输入pip.exe install psycopg2。注意是pip.exe,和替换自己的模块名。如果安装速度过慢,可以参考https://hanbo./article/details/106068605,配置国内镜像加速回到ArcGIS中,测试Python工具箱,已经没有错误了。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。