程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

ArcGIS Python工具箱集成第三方模塊的解決辦法

編輯:Python

在ArcGIS中開發Python工具箱時,引入ArcPy開發一些空間處理工具和邏輯是沒有問題。但是,在更復雜的場景下,就需要依賴其他的Python模塊來實現,例如讀寫word、查詢PostgreSQL數據庫等。在Python工具箱的代碼裡引入其他Python模塊時,語法檢查中,會報下面的錯誤,代碼如下:

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

錯誤:

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

解決方法

  1. 找到ArcGIS安裝後,自帶的python2.7環境下的Scripts目錄,比如我的是C:\Python27\ArcGIS10.8\Scripts
  2. 在文件地址欄中輸入cmd,然後按回車

  1. 在彈出的cmd命令窗口中,輸入pip.exe install psycopg2。注意是pip.exe,和替換自己的模塊名。如果安裝速度過慢,可以參考https://hanbo.blog.csdn.net/article/details/106068605,配置國內鏡像加速
  2. 回到ArcGIS中,測試Python工具箱,已經沒有錯誤了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved