Shasha99 Shasha99 - 3 months ago 18
Python Question

ImportError: cannot import name 'VIServer'

I am using python 2.7.6 and pysphere 0.1.7.I am getting the error in the following code:

import sys
import pysphere
from pysphere import VIServer
if vm_target.get_status() == 'POWERED OFF':

while vm_target.is_powering_on():

The error says: ImportError: cannot import name 'VIServer'

The script is trying to copy a file from the local machine to the target VM.

The full error Message is:

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

Any help would be kind;


EDIT: Valid but not the correct problem

if you're already importing pyshpere, why not use

If that isn't what you want you'll have to post more code

It looks like you have named your python script

When you run from pysphere import VIServer, that imports a long chain of things until it gets to:

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

From here, python uses a depth-first search to find a module named Where might that be? Of course! it's right there in front of it. So now python re-imports your module because it is named Here, python realizes something has gone very wrong and it is now re-importing the stuff ti already did. That's no good so it quits.

If you want to avoid this, you need to either use

server=pysphere.VIServer(), or rename your file, or both.

In general your files should be named something very descriptive, so renaming is probably the best way. Just remember if you rename it to something that exists outside of the default python language (Like, say I name a script MatPlotLib), it'll import yours before it finds the real one!