PyCharm autocomplete does not work with pygame - python

I've just installed PyCharm Community Edition 3.4.1 and tried to make a simple pygame project in it. I found that code completion runs in a weird way. In this case:
from pygame import event
event.
when I type event. a completion popup with event methods shows immediately. But in the second case:
import pygame
pygame.event.
a popup contains only object methods.
How can I learn the autocomplete tool to look deeper into the library?

Other than creating your own skeletons, you can't. You can make pycharm a little better a code completion if you enable the following:
But other than that, you're out of luck. Python is hard to make code completion for because its a dynamic language, and stubs (skeletons) don't exist for everything.

I tried Daid's answer (removing the try/except in init.py) and it didn't work, but it was very close! Here is how you can fix it specifically for pygame:
Go to your pygame folder and open init.py in a text editor
Navigate to the import section with the try/except clauses (around line 109)
Change the format from import pygame.module to from pygame import module for the modules you want
For example, change
try: import pygame.event
to
try: from pygame import event
Restart PyCharm and it should work :)

It has to do with how pygame is constructed.
The:
python\Lib\site-packages\pygame\__init__.py
File contains the following construction:
try: import pygame.cdrom
except (ImportError,IOError):cdrom=MissingModule("cdrom", geterror(), 1)
Which allows missing imports. However, this confuses pycharm. Removing the try+except will fix the pycharm auto completion.

Related

Questions about errors from Pylint

I'm trying to use pygame for the first time by following a tutorial on Youtube.
I realized that Pylint points out a lot of errors that I think are irrelevent, such as "C0114:missing-module-docstring". Is there a way to "customize" what Pylint displays ? I find myself with a lot of blue underlining in my whole code which is unpleasant, and I don't want to uninstall Pylint because it can be handy when I make a typo, for example.
There is also an error that I don't understand : "E0611:no-name-in-module". This is shown at the beginning of my code when I try to import a module from pygame :
from pygame.math import Vector2
This is strange because my game runs using this module without any problems. It also occurs when I call functions from pygame such as :
pygame.init()
Again, it works, but it's underlined in red and shows an error. Is there any way to fix this ?
I tried to go in the settings to see if I could modify something regarding Pylint errors, with no success.
You can control what checks pylint does with a separate .pylintrc configuration file, just add one to your project root and pylint should use it automatically. You can create one for yourself from scratch, use the builtin generator with pylint --generate-rcfile or try finding a ready template on the web such as:
https://www.codeac.io/documentation/pylint-configuration.html
https://gist.github.com/rpunkfu/3cd6f8b8b23e27adedbd

How to enable the quick fix for auto import using vscode for python and PySide6

I use vscode as my python IDE, and had installed some nessary plugins such as python and pylance for it. But now I have a problem, the pylance for vscode could not reconizge the PySide6 modules for quick fix auto import, like following picture shows:
But the quick fix work for other modules.
Could someone help to point out how to resolve it? It worked before but one day could not work any more.
Sorry, but normally, we will not act like this. We will not type a complex or long className then import it through a quick fix. at most we type sys and import it immediately.

PyCharm can't find queue.SimpleQueue

Using pycharm with python 3.7. I am using queue.SimpleQueue. The code runs fine, and PyCharm is pointed at the correct interpreter and all that. But with this code:
import queue
Q = queue.SimpleQueue()
I get a warning "Cannot find reference 'SimpleQueue' in 'queue.pyi'".
I do some searching. I hit ctrl-B on the "import queue" statement and it takes me to a file called queue.pyi in the folder helpers/typeshed/stdlib/3/ under the pycharm installation. So apparently instead of the queue.py file in lib/python3.7/ under the python venv, it thinks I'm trying to import this queue.pyi file instead, which I didn't even know existed.
Like I said, the code runs fine, and I can simply add # noinspection PyUnresolvedReferences and the warning goes away, but then the type inferencing and code hints on the variable Q don't work.
Another fix is to instead import _queue and use _queue.SimpleQueue, because apparently in python 3.7 queue.SimpleQueue is implemented in cython and is imported from a cython package _queue. But importing _queue seems hackish and implementation-dependent.
Is there a way to tell PyCharm that import queue means the actual lib/python3.7/queue.py as opposed to whatever helpers/typeshed/stdlib/3/queue.pyi is?
It was fixed in PyCharm 2019.3 https://youtrack.jetbrains.com/issue/PY-31437, could you please try to update?

How do I import pygame on 3.7.1?

I'm running python 3.7.1 on my MacBook and it has been running perfectly until I tried importing pygame. I'm taking an intro to python class and we always just do from module import *. When I tried to do that same format with pygame I just got an error ModuleNotFoundError: No module named 'pygame'
What should I do about using pygame or should I just forget using pygame altogether
I'm new to python and the reason I'm trying to use pygame is that I'm making a game where the user controls a .gif or a spaceship and tries to avoid asteroids. All of my code is solid accept for the collision detection. Everyone seems to point me towards using pygame but i can't get that to work. Should I continue to use pygame and if so, how do i fix my problem? Should I drop pygame altogether and just try collision detection a different way; (if so, what other way?)
pygame is not bundled in standard python.
to install pygame in Mac, see
Mac installation from pygame.org
Also, from module import * is not a good practice if you're using more modules,
which will causes class name conflicts.
Easiest way to do it - forget 3.7.1 go on the Python website (linked) and download 3.6.7 ther when your setting up click add to PATH then you need to install pygame ( save it locally near where python is saved) then go on to terminal by going to finder and clicking applications and search terminal and type pip install pygame if there is anything you are confused about just ask (:
Path button

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__.py
What's the right way to enable autocomplete?
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.
Try:
from pygame import *
Now you can see nested autocompletion options.

Categories

Resources