8

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?

Ivan Smirnov
  • 4,365
  • 19
  • 30

3 Answers3

5

Other than creating your own skeletons, you can't. You can make pycharm a little better a code completion if you enable the following:

enter image description here

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.

Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
  • 1
    Why can't the IDE make use of ipython, which has awesome autocompletion? – qed Dec 28 '14 at 20:10
  • Because iPython is a console, and PyCharm's editor is an editor. In iPython, what you're actually doing is running a heavy debugger too, to inspect all the stuff that you're asking python to do. – Games Brainiac Jan 01 '15 at 07:51
4

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:

  1. Go to your pygame folder and open init.py in a text editor
  2. Navigate to the import section with the try/except clauses (around line 109)
  3. 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 :)

Bee Kay
  • 166
  • 7
  • I've changed the import statements as you suggested and in addition triggered File → Invalidate Caches / Restart (took over 30 minutes!) and now autocomplete works for pygame (mostly). Unfortunately, PyCharm still collects errors like this during inspection: "Cannot find reference 'load' in 'image.py'. Thanks, anyways! – pixelperfect Mar 21 '17 at 15:37
1

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.

Daid
  • 11
  • 1