2016-01-20 134 views
0

我使用python 2.7.6和pysphere 0.1.7.I我得到下面的代碼中的錯誤:導入錯誤:無法導入名稱「VIServer」

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() 

錯誤說:導入錯誤:無法導入名稱'VIServer'

腳本正試圖將文件從本地計算機複製到目標虛擬機。

完整的錯誤信息是:

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是腳本的名稱。

任何幫助將是善良的;

+0

當你刪除「import pysphere」這一行並且剛剛從'pysphere import VIServer'行刪除了這行時會發生什麼? – Wolf

+0

@Wolf,它給出了同樣的錯誤; – Shasha99

+0

你能否包含完整的錯誤信息?你是否將腳本命名爲pysphere.py? – M4rtini

回答

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意識到某些東西已經非常錯誤,現在它已經重新導入了已經做過的東西。這是不好的,所以它退出。

如果你想避免這種情況,你需要或者使用

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

通常你的文件應該被命名爲非常具有描述性的東西,所以重命名可能是最好的方法。只要記住,如果你將它重命名爲默認Python語言之外的東西(比如說,我命名了一個腳本MatPlotLib),它會在你找到真正的語言之前導入你的東西!

+0

編輯該問題(添加了代碼)。請檢查; – Shasha99

+0

嘗試重命名您的腳本。看起來像試圖導入它 –

+0

重命名工作。謝謝 ! – Shasha99

相關問題