0

我正在使用 python 2.7.6 和 pysphere 0.1.7。我在以下代码中收到错误:

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: cannot import name '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 是脚本名称。

任何帮助都是善意的;

4

1 回答 1

1

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

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

pysphere.VIServer.foo()

如果那不是您想要的,您将不得不发布更多代码


看起来你已经命名了你的 python 脚本copy.py

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

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

从这里开始,python 使用深度优先搜索来查找名为 copy.py 的模块,它可能在哪里?当然!它就在它的前面。所以现在 python 重新导入你的模块,因为它被命名为copy.py. 在这里,python 意识到出了点问题,它现在正在重新导入 ti 已经做的事情。这不好,所以放弃了。

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

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

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

于 2016-01-20T13:46:28.063 回答