【问题标题】:Cannot import ArcPy from ArcGIS Pro 2.5.2 in PyCharm无法在 PyCharm 中从 ArcGIS Pro 2.5.2 导入 ArcPy
【发布时间】:2020-11-14 12:40:03
【问题描述】:

我安装了 ArcGIS Pro 2.5.2,并使用位于此处的解释器在 PyCharm 中创建了一个新的虚拟环境:

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe

我已将 ArcGISPro.pth 文件复制到我的虚拟环境中,以便它知道 ArcPy 的位置。当我运行一个简单的脚本时:

import arcpy

我得到以下,有点难以理解的错误:

C:\Users\lharris\PycharmProjects\arcpro_test\venv\Scripts\python.exe C:/Users/lharris/PycharmProjects/arcpro_test/test.py
arcpy must be run from inside the Conda environment, which ArcGIS
Traceback (most recent call last):
uses to manage the installation of Python.
  File "C:/Users/lharris/PycharmProjects/arcpro_test/test.py", line 1, in <module>
You can access this environment by launching Python from the links inside the
    import arcpy
"ArcGIS > ArcGIS Pro" Start Menu Group:
    "Python Interactive Terminal" starts a Python session.
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\__init__.py", line 70, in <module>
    "Python Command Prompt" opens a Command Prompt window initialized with Conda.

Or by running the proenv.bat file in the terminal:
    raise ImportError("arcpy needs to run within an active ArcGIS Conda environment")
     C:\> C:\Program Files\ArcGIS\Pro\bin\python\scripts\proenv.bat

ImportError: arcpy needs to run within an active ArcGIS Conda environment

Process finished with exit code 1

当我用那个 Python 解释器打开一个终端时,它会说:

Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated. Libraries may fail to load.  To activate this environment
please see https://conda.io/activation

我相信 ArcGIS Pro 已经更新(这是在工作的服务器上,所以我无法控制它)。但是,我以前导入 ArcPy 没有问题。那么,这可能是此更新版本特有的新问题吗?是不是跟 Arc 使用 Conda 有关系?

如何使用与 ArcGIS Pro 2.5.2 捆绑的 Python 版本和 PyCharm?

【问题讨论】:

    标签: python pycharm arcgis arcpy


    【解决方案1】:

    根据link,环境名称为arcgispro-py3

    因此,我假设您的环境名称是arcgispro-py3

    1- 打开终端或命令提示符并输入:conda activate arcgispro-py3。如果这条语句执行成功。你会看到这样的屏幕:

    如果执行后出现错误,则表示您的环境名称不同。您需要将环境名称替换为 arcgispro-py3

    2- 现在输入python

    3- 并输入import arcpy 输入:

    如果第3步出现错误,则通过exit()退出python并安装环境:conda install -c esri arcpy

    4- 再次输入import arcpy

    现在我认为这将解决您的问题,否则您可能需要重新安装 ArcGIS。

    【讨论】:

    • 感谢您的回答。我不能这样做,因为我不使用 conda,它没有安装在有问题的机器上,所以我收到 'conda' is not recognized as an internal or external command 错误。我使用 PyCharm 作为我的 IDE,它内置在基于 pip 的包管理器中。我的问题是如何在 PyCharm 创建的虚拟环境中导入 arcpy - 直到最近我才能做到这一点。
    猜你喜欢
    • 2021-03-04
    • 2016-06-26
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 2020-06-13
    • 2015-06-30
    • 2020-10-03
    相关资源
    最近更新 更多