【问题标题】:call a script in Python 2 and Python 3 is different?在 Python 2 和 Python 3 中调用脚本有什么不同?
【发布时间】:2021-09-08 14:27:21
【问题描述】:
import sys
sys.path.append(r'E:\Python')
from hypy import htj
tD = np.array([[1, 2, 3],[4, 5, 6]])
x = 0.1
sD, _ = htj.dls_array(np.array([x]), tD)

AttributeError: 'module' 对象没有属性 'htj'

当我在 Spyder Python(3.8 版)中运行上述代码时,我得到了结果。但是,我在 ArcGIS Desktop Python(版本 2)中运行它时遇到了困难,我需要在其中运行它。

hypy 文件夹位于 E:\Python 内。
htj.py 是一个脚本,具有位于 hypy 文件夹内的 dls_array 函数。

sD 后面的“_”是有目的的。

请提出一种处理方法,因为由于软件不可用,我无法从 ArcGIS Desktop 切换到 ArcGIS Pro Python。

【问题讨论】:

  • hypy/ 中是否有__init__.py?也尝试sys.path.insert(0, r'E:\Python') 以确保它在路径上的第一个并且不会被其他东西覆盖。一般来说,您不应该使用sys.path 操作,而是将您的包安装到标准路径位置。但我们稍后会过那座桥......
  • @Iguananaut 是的,hypy 文件夹中有一个 init.py 文件。我尝试使用 sys.path.insert(0,r'E:\Python') 但我收到了同样的错误。 AttributeError:“模块”对象没有属性“htj”。
  • 尝试自己运行import hypy,然后在你的代码print(hypy) 中查看模块是从哪里导入的。
  • @Iguananaut >>> import hypy >>> print(hypy) init_.pyc'> 这是什么按照您的建议运行后我收到了。
  • 好的,如果你运行import hypy.htj会发生什么? (请尝试在反引号 so it looks like this 中包含路径之类的内容,并且不会出现奇怪的格式),这样我们就可以准确地看到您的输出是什么。

标签: python-2.7 arcgis


【解决方案1】:

试试这些:

  1. 确保在文件夹E:\Python 中有一个__init__.py 空文件
  2. 试着明确地说from .hypy import htj
  3. 尝试:from hypy.htj import dls_array,然后在不使用htj. 的情况下使用dls_array 4 最重要的一点:尽量避免使用 Python2。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-20
    • 2012-04-07
    • 2020-05-06
    • 2017-04-23
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多