How to enable autocomplete (IntelliSense) for python package modules?

廉价感情. 提交于 2019-12-04 10:08:01

I've found a solution, but I would appreciate some explanation from someone more experienced with Python:

import package.module as module

With Pygame mixer it's:

import pygame.mixer as mixer 

But I still don't understand why autocomplete for import.package.module doesn't work, but import.package.module as module does.

Probably pygame.mixer doesn't work with import pygame because there is no attribute mixer inside pygame package. If attribute is not there autcomplete won't list it.

When you import package, Python doesn't recursively import subpackages and modules unless it's explicitly assigned inside the "__init__.py" file inside a package.

This is why import pygame.mixer as mixer works because you import pygame package and mixer module(?) which is available through local name mixer. However with such import you don't have pygame available in local scope.

Similar situation is when you just import pygame.mixer. pygame is available but mixer has to be referenced by pygame.mixer.

In both cases pygame package and pygame.mixer module(?) are executed.

You could also use from pygame import mixer instead import pygame.mixer as mixer or from pygame import mixer as module if you want to rename.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!