vmware / pyvmomi

VMware vSphere API Python Bindings
Apache License 2.0
2.22k stars 766 forks source link

Invalid automatic type import #1025

Open IvarVirusiim opened 1 year ago

IvarVirusiim commented 1 year ago

Describe the bug

When trying to import using PyCharm, using for example VirtualMachine, it imports pyVmomi.vim.VirtualMachine, but the correct import is vim.VirtualMachine. This causes errors during runtime.

Reproduction steps

  1. Try to import some type automatically image

  2. Run the code and get error: ImportError: cannot import name 'VirtualMachine' from 'pyVmomi.vim' (unknown location)

Expected behavior

Import would be correct: from vim import VirtualMachine

Additional context

No response

mk-fg commented 1 year ago

I have similar issue with this code:

import pyVmomi.vim as vim
print(vim.Task)
# Result (unexpected): AttributeError: module 'pyVmomi.vim' has no attribute 'Task'

While this equivalent-at-a-glance code works:

from pyVmomi import vim
print(vim.Task)
# Result (expected): <class 'pyVmomi.VmomiSupport.vim.Task'>

Furthermore, if e.g. import pyVmomi.vim is called first, it breaks the module within that python runtime, for example:

import pyVmomi.vim

from pyVmomi import vim
print(vim.Task)
# Result (unexpected): AttributeError: module 'pyVmomi.vim' has no attribute 'Task'

If this is not easy to fix, I'd recommend adding something like raise ImportError('Use "from pyVmomi import vim" ...') when module is imported via import X, which should be detectable via same missing class-attribute lookups.

This way, at least it'd be clear what the issue is, as otherwise it looks like module and all example code for it is outdated or broken in current pip releases, as it tries to use no-longer-existant module attributes.