Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python、comtypes和ArcObjects:创建Apport对象时出错_Python_Arcobjects_Comtypes - Fatal编程技术网

Python、comtypes和ArcObjects:创建Apport对象时出错

Python、comtypes和ArcObjects:创建Apport对象时出错,python,arcobjects,comtypes,Python,Arcobjects,Comtypes,我正在Python 2.6.5和ArcGIS 10 SP1下试验comtypes和ArcObjects。我使用纯Python方法包装中描述的ArcObjects OLB,但在comtypes.CoCreateInstance方法中得到一个错误 以下是我正在运行的代码: def WrapModules(): #force wrapping of all ArcObjects libraries (OLBs) import os import comtypes.client

我正在Python 2.6.5和ArcGIS 10 SP1下试验
comtypes
和ArcObjects。我使用纯Python方法包装中描述的ArcObjects OLB,但在
comtypes.CoCreateInstance
方法中得到一个错误

以下是我正在运行的代码:

def WrapModules():
    #force wrapping of all ArcObjects libraries (OLBs)
    import os
    import comtypes.client
    # change com_dir to whatever it is for you
    com_dir = r'C:\Program Files\ArcGIS\Desktop10.0\com'
    coms = [os.path.join(com_dir, x) for x in os.listdir(com_dir) if os.path.splitext(x)[1].upper() == '.OLB']
    map(comtypes.client.GetModule, coms)

def GetApp():
    """Get a hook into the current session of ArcMap"""
    from comtypes.gen import esriFramework
    pAppROT = NewObj(esriFramework.AppROT, esriFramework.IAppROT)
    if pAppROT is not None:
        iCount = pAppROT.Count
        if iCount == 0:
            print 'No ArcGIS application currently running.  Terminating ...'
            return None
        for i in range(iCount):
            pApp = pAppROT.Item(i)  #returns IApplication on AppRef
            if pApp.Name == 'ArcMap':
                print "ArcMap found"
                return pApp
        print 'No ArcMap session is running at this time.'
    print "No AppROT found"
    return None

def NewObj(MyClass, MyInterface):
    """Creates a new comtypes POINTER object where\n\
    MyClass is the class to be instantiated,\n\
    MyInterface is the interface to be assigned"""
    from comtypes.client import CreateObject
    import traceback
    try:
        ptr = CreateObject(MyClass, interface=MyInterface)
        return ptr
    except:
        print traceback.format_exc()
        return None

if __name__ == "__main__":
    WrapModules()
    pApp = GetApp()
    if pApp is not None:
        print "HWND: %d" % pApp.hWnd
    else:
        print "No ArcGIS application found!"
下面是脚本的输出:

Traceback (most recent call last): File "C:\temp\ComHelpers.py", line 35, in NewObj ptr = CreateObject(MyClass, interface=MyInterface) File "C:\Python26\ArcGIS10.0\lib\site-packages\comtypes\client\__init__.py", line 235, in CreateObject obj = comtypes.CoCreateInstance(clsid, clsctx=clsctx, interface=interface) File "C:\Python26\ArcGIS10.0\lib\site-packages\comtypes\__init__.py", line 1145, in CoCreateInstance _ole32.CoCreateInstance(byref(clsid), punkouter, clsctx, byref(iid), byref(p)) File "_ctypes/callproc.c", line 925, in GetResult WindowsError: [Error -2147221231] ClassFactory cannot supply requested class No AppROT found No ArcGIS application found! 回溯(最近一次呼叫最后一次): 文件“C:\temp\ComHelpers.py”,第35行,在NewObj中 ptr=CreateObject(MyClass,interface=MyInterface) CreateObject中第235行的文件“C:\Python26\ArcGIS10.0\lib\site packages\comtypes\client\\uuuuu init\uuuuu.py” obj=comtypes.CoCreateInstance(clsid,clsctx=clsctx,interface=interface) CoCreateInstance中的文件“C:\Python26\ArcGIS10.0\lib\site packages\comtypes\\uuuuuu init\uuuuuuu.py”,第1145行 _ole32.CoCreateInstance(byref(clsid)、punkouter、clsctx、byref(iid)、byref(p)) 文件“\u ctypes/callproc.c”,第925行,在GetResult中 WindowsError:[错误-2147221231]ClassFactory无法提供请求的类 找不到许可证 找不到ArcGIS应用程序! 感谢您的任何见解

为完整起见,由Jason Scheirer在GIS堆栈交换上发布:

首先导入arcpy,您没有进行任何许可证签出,也没有按原样设置ArcObjects 10.0运行时,因此它找不到CoClass

为了完整起见,Jason Scheirer在GIS堆栈交换上发布了:

首先导入arcpy,您没有进行任何许可证签出,也没有按原样设置ArcObjects 10.0运行时,因此它找不到CoClass