Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This question is not about Pygame, I'm usin Pygame as an example.

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__.pyenter image description here What's the right way to enable autocomplete?

like image 226
Braca Avatar asked Jun 24 '18 01:06

Braca


People also ask

Does IntelliSense work with Python?

The Python extension supports code completion and IntelliSense using the currently selected interpreter. IntelliSense is a general term for a number of features, including intelligent code completion (in-context method and variable suggestions) across all your files and for built-in and third-party modules.

How do I enable autocomplete in Python idle?

Code Completion and Call Tips Python IDLE has basic code completion functionality. It can only autocomplete the names of functions and classes. To use autocompletion in the editor, just press the tab key after a sequence of text. The call tip will display as a popup note, reminding you how to append to a list.

How do I get IntelliSense code for Visual Studio Python?

On your VS code use the command ctr + shift + P then search for Python: Select Lint and choose your preferred linting tool.


2 Answers

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 

enter image description here

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

like image 170
Braca Avatar answered Sep 29 '22 15:09

Braca


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.

like image 35
WloHu Avatar answered Sep 29 '22 16:09

WloHu