如何让 PyC​​harm IDE 为 pygame 的子模块完成代码? [英] How to get PyCharm IDE to do code completion for pygame's sub-modules?

查看:60
本文介绍了如何让 PyC​​harm IDE 为 pygame 的子模块完成代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介:pygame 是一个用于创建游戏的 Python 模块.我已经正确安装了该模块并且我的 PyCharm 成功导入了它.

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:

尝试对 pygame 下的子模块image"使用 get 代码完成工作正常:

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

我已经尝试了很多年了.PyCharm 中的 python 控制台可以做到这一点(它使用 IPython,如果我错了,请纠正我)但编辑器不能.

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 抱歉没有显示上面的截图,我没有足够的 stackoverflow 点来发布图片

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

推荐答案

以下是专门针对 pygame 解决此问题的方法:

Here is how you can fix this issue specifically for pygame:

  1. 转到您的 pygame 文件夹并在文本编辑器中打开 __init__.py
  2. 使用 try/except 子句导航到导入部分(第 109 行左右)
  3. 将格式从 import pygame.module 更改为 from pygame import module 以获取所需模块
  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

例如改变

try: import pygame.event

try: from pygame import event

重启 PyCharm,它应该可以工作了 :)

Restart PyCharm and it should work :)

这篇关于如何让 PyC​​harm IDE 为 pygame 的子模块完成代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆