How to get PyCharm IDE to do code completion for p

2019-07-21 23:50发布

问题:

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

回答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 :)



回答2:

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.



回答3:

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.