2

Intro: pygame is a python module used to create games. I've properly installed the module and my PyCharm successfully imports it.

I'll jump straight to an example scenario:

Trying to use the get code completion for the sub-module "image" under pygame works fine:

pygame.image

When I type further and try to get code completion under image, it doesn't work:

pygame.image.  # shows a suggestions list, but list doesn't contain any
               # function names under the pygame.image sub-module

I've been trying for ages. The python console within PyCharm can do this (it uses IPython, correct me if I'm wrong) but the editor cannot.

PS apologies for not showing screenshots for the above, I don't have enough stackoverflow points to post images

Bee Kay
  • 166
  • 7
  • JetBrains is a generic IDE. The same auto-complete feature for IntelliJ is the same auto-complete they use for PyCharm and all their other environments. Thus auto-completions are derived from your existing code, not from the language's modules. – Malik Brahimi Mar 04 '15 at 17:39
  • @MalikBrahimi Hmm that could be the case, because I successfully get code completion in NinjaIDE and IPython and IDLE. Just not PyCharm. So is there no way for PyCharm to recursively search through all sub-modules inside an imported module, and help with code completion? – Bee Kay Mar 04 '15 at 18:17
  • Can you please mark as answer if my response was helpful to you? – Malik Brahimi Mar 04 '15 at 20:12
  • Are you working in a virtual environment or have ou installed pygame to your system Python? – kylieCatt Mar 05 '15 at 00:31
  • @IanAuld It is installed to my system python – Bee Kay Mar 05 '15 at 05:30
  • Make sure that in the settings PyCharm is pointed to the right Pyhton interpretor. – kylieCatt Mar 05 '15 at 21:36
  • Possible duplicate of [How do I import modules in pycharm?](https://stackoverflow.com/questions/19885821/how-do-i-import-modules-in-pycharm) – Pedram May 25 '19 at 05:59

3 Answers3

1

Here is how you can fix this issue 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
1

Here's my terrible solution...I basically import the ones I feel I'd need and if I need something like a clock I'll just add it to the top if I'm going to be using it a lot.

import pygame

import pygame.draw as draw
import pygame.cursors as cursors
import pygame.math as math
import pygame.display as display
import pygame.event as event
import pygame.image as image
import pygame.joystick as joystick
import pygame.key as key
import pygame.mouse as mouse
import pygame.sprite as sprite
import pygame.threads as threads

It looks bad but autocomplete works.

-1

JetBrains is a generic IDE. The same auto-complete feature for IntelliJ is the same auto-complete they use for PyCharm and all their other environments. Thus auto-completions are derived from your existing code, not from the language's modules. See here for more.

Community
  • 1
  • 1
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
  • That said, I believe if you add the package directory to your project, the IDE will index them and provide some level of auto complete... problem is, is I don't know how to do this off hand... – Frank V Mar 04 '15 at 19:55
  • The link to the other post has some possible solutions, thank you. I would suggest you to edit your answer to include that PyCharm does have auto-complete for imported modules, but it is just the *sub-modules* for which you won't get auto-complete to work (unless each of them is individually imported) – Bee Kay Mar 04 '15 at 20:17