ArcGIS 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工具箱,已经没有错误了。
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GIS开发者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值