ImportError: cannot import name 'VIServer'

ImportError: cannot import name 'VIServer'

我正在使用 python 2.7.6 和 pysphere 0.1。7.I 在以下代码中遇到错误:

import sys
import pysphere
from pysphere import VIServer
server=VIServer()
server.connect(host,login,password)
vm_target=server.get_vm_by_name(guest)
if vm_target.get_status() == 'POWERED OFF':
        vm_target.power_on()

while vm_target.is_powering_on():
        continue
server.disconnect()

错误说:ImportError: 无法导入名称 'VIServer'

脚本正在尝试将文件从本地计算机复制到目标 VM。

完整的错误消息是:

Traceback (most recent call last):
  File "copy.py", line 4, in <module>
    from pysphere import VIServer
  File "/usr/local/lib/python2.7/dist-packages/pysphere/__init__.py", line 171, in <module>
    from pysphere.vi_task import VITask
  File "/usr/local/lib/python2.7/dist-packages/pysphere/vi_task.py", line 34, in <module>
    from pysphere.resources import VimService_services as VI
  File "/usr/local/lib/python2.7/dist-packages/pysphere/resources/VimService_services.py", line 6, in <module>
    from pysphere.resources.VimService_services_types import *
  File "/usr/local/lib/python2.7/dist-packages/pysphere/resources/VimService_services_types.py", line 7, in <module>
    import pysphere.ZSI
  File "/usr/local/lib/python2.7/dist-packages/pysphere/ZSI/__init__.py", line 151, in <module>
    from pysphere.ZSI.wstools.Namespaces import ZSI_SCHEMA_URI
  File "/usr/local/lib/python2.7/dist-packages/pysphere/ZSI/wstools/__init__.py", line 7, in <module>
    from pysphere.ZSI.wstools import WSDLTools
  File "/usr/local/lib/python2.7/dist-packages/pysphere/ZSI/wstools/WSDLTools.py", line 15, in <module>
    from pysphere.ZSI.wstools.Utility import Collection, CollectionNS, DOM, ElementProxy, basejoin
  File "/usr/local/lib/python2.7/dist-packages/pysphere/ZSI/wstools/Utility.py", line 36, in <module>
    import xml.dom.minidom
  File "/usr/lib/python2.7/xml/dom/minidom.py", line 22, in <module>
    from xml.dom.xmlbuilder import DOMImplementationLS, DocumentLS
  File "/usr/lib/python2.7/xml/dom/xmlbuilder.py", line 3, in <module>
    import copy
  File "/home/shasha/devOps/pythonSamples/copy.py", line 4, in <module>
    from pysphere import VIServer
ImportError: cannot import name VIServer

copy.py是脚本名称。

如有帮助,将不胜感激;

编辑:有效但不是正确的问题

如果您已经在导入 pyshpere,为什么不使用

pysphere.VIServer.foo()

如果这不是您想要的,您将不得不 post 更多代码


您似乎已将 python 脚本命名为 copy.py

当您 运行 from pysphere import VIServer 时,会导入一长串东西,直到到达:

File "/usr/lib/python2.7/xml/dom/xmlbuilder.py", line 3, in <module>
import copy

从这里开始,python 使用 depth-first 搜索找到一个名为 copy.py 的模块,它可能在哪里?当然!它就在它的前面。所以现在 python re-imports 你的模块,因为它被命名为 copy.py。在这里,python 意识到出了点问题,现在 re-importing 我已经做过了。那不好,所以它退出了。

如果你想避免这种情况,你需要使用

server=pysphere.VIServer(),或重命名您的文件,或两者兼而有之。

一般来说,您的文件应该命名为具有描述性的名称,因此重命名可能是最好的方法。请记住,如果您将它重命名为默认 python 语言之外的东西(比如,假设我将脚本命名为 MatPlotLib),它会在找到真正的语言之前导入您的脚本!