我使用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:无法导入名称'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 VIServercopy.py是脚本名。
任何帮助都是好心的;
发布于 2016-01-20 13:46:28
编辑:有效但不正确的问题
如果你已经在进口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意识到有些事情发生了很大的错误,现在它正在重新导入已经做过的事情。这样不好,所以就退出了。
如果您想避免这种情况,您需要使用
server=pysphere.VIServer(),或者重命名您的文件,或者两者都重命名。
一般来说,您的文件应该被命名为非常具有描述性的东西,因此重命名可能是最好的方式。请记住,如果您将它重命名为默认python语言之外存在的东西(比如,我给脚本命名为MatPlotLib),它会在找到真正的脚本之前导入您的脚本!
https://stackoverflow.com/questions/34901525
复制相似问题