如何为python软件包模块启用自动完成功能(IntelliSense)? [英] How to enable autocomplete (IntelliSense) for python package modules?
问题描述
这个问题与Pygame无关,我以Pygame为例.
在尝试Pygame时,我注意到自动完成功能不适用于某些模块.例如,如果我开始输入pygame.mixer
,则自动完成功能将显示MissingModule
.在寻找解决方案时,对于使用C语言编写的各种文本编辑器和模块,我发现了很多类似的问题.我使用的是Visual Studio Code,python路径设置正确,我的代码运行良好.
一种奇怪的解决方法是修改Pygame的__init__.py
启用自动完成功能的正确方法是什么?
While experimenting with Pygame I've noticed that autocomplete is not working for some modules. For example, if I start typing pygame.mixer
autocomplete shows MissingModule
. While searching for a solution I've found a lot of similar questions for various text editors and modules that have parts written in C. I am using Visual Studio Code, python path is set correctly and my code runs fine.
One strange workaround is modifying Pygame's __init__.py
What's the right way to enable autocomplete?
推荐答案
pygame.mixer
可能不适用于import pygame
,因为pygame
package 中没有属性mixer
.如果不存在该属性,则autcomplete不会列出该属性.
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.
导入包时,Python不会递归地导入子包和模块,除非在包内的"__init__.py"文件中明确指定了子包和模块.
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.
这就是为什么import pygame.mixer as mixer
起作用的原因,因为您导入了pygame
软件包和 mixer
模块(?),该模块可通过本地名称mixer
获得.但是,使用这种导入,您在本地范围内没有pygame
可用.
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.
类似的情况是您只是import pygame.mixer
. pygame
可用,但mixer
必须由pygame.mixer
引用.
Similar situation is when you just import pygame.mixer
. pygame
is available but mixer
has to be referenced by pygame.mixer
.
在两种情况下都执行pygame
软件包和pygame.mixer
模块(?).
In both cases pygame
package and pygame.mixer
module(?) are executed.
如果要重命名,也可以使用from pygame import mixer
代替import pygame.mixer as mixer
或from pygame import mixer as module
.
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.
这篇关于如何为python软件包模块启用自动完成功能(IntelliSense)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!