Python 为什么说 pygame 模块没有 init 成员?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50569453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does it say that module pygame has no init member?
提问by Ludde
This is the code I have:
这是我的代码:
import pygame
pygame.init()
I'm very confused because if I try to run the file, then there seems to be no issue, but pylint says the following:
我很困惑,因为如果我尝试运行该文件,那么似乎没有问题,但是 pylint 说以下内容:
E1101:Module 'pygame' has no 'init' member
E1101:模块“pygame”没有“init”成员
I have searched thoroughly for a solution to this "error". In every relevant case I found, the solution was to make sure that I have not made another file or folder with the name "pygame", because in that case, I would just be importing my own file or folder.
我已经彻底搜索了这个“错误”的解决方案。在我发现的每个相关案例中,解决方案是确保我没有创建另一个名为“pygame”的文件或文件夹,因为在这种情况下,我只会导入我自己的文件或文件夹。
However, I have not made a folder or file with a name even close to "pygame", so I don't know what the problem is.
但是,我还没有创建名称甚至接近“pygame”的文件夹或文件,所以我不知道问题是什么。
As said earlier, it seems like I'm able to run the file without any issues and having errors like this confuses me in my learning process.
如前所述,似乎我可以毫无问题地运行该文件,并且在我的学习过程中出现这样的错误使我感到困惑。
I write code in Visual Studio Code, I'm using python 3.6, I'm using pygame 1.9.3 and have updated my pylint. Any help would be appreciated.
我在 Visual Studio Code 中编写代码,我使用的是 python 3.6,我使用的是 pygame 1.9.3 并更新了我的 pylint。任何帮助,将不胜感激。
回答by Parkofadown
If you have VS code, go in your .vscode folder > settings.json or search for python.linting.mypyArgs Under user settings tab paste inbetween curly braces
如果您有 VS 代码,请进入您的 .vscode 文件夹 > settings.json 或搜索 python.linting.mypyArgs 在用户设置选项卡下粘贴在大括号之间
"python.linting.pylintArgs": [
"--extension-pkg-whitelist=lxml" // The extension is "lxml" not "1xml"
]
I no longer see the pyinit error.
我不再看到 pyinit 错误。
回答by vt.
Summarizing all answers.
总结所有答案。
This is a security measure to not load non-default C extensions.
这是一种不加载非默认 C 扩展的安全措施。
You can white-list specific extension(s).
Open user settings and add the following between {}:
"python.linting.pylintArgs": [ "--extension-pkg-whitelist=extensionname" // comma separated ]
You can allow to "unsafe load" all extensions.
Open user settings and add the following between {}:
"python.linting.pylintArgs": [ "--unsafe-load-any-extension=y" ]
您可以将特定扩展名列入白名单。
打开用户设置并在 {} 之间添加以下内容:
"python.linting.pylintArgs": [ "--extension-pkg-whitelist=extensionname" // comma separated ]
您可以允许“不安全加载”所有扩展。
打开用户设置并在 {} 之间添加以下内容:
"python.linting.pylintArgs": [ "--unsafe-load-any-extension=y" ]
回答by CompuGenuis Programs
I had the same issue when I started using Visual Studio Code with Python. It has nothing to do with having another pygame.py or not installing it properly. It has to do with the fact that Visual Studio Code takes your code literally, and since you cannot import pygame.init()
, it thinks that it isn't a correct module.
当我开始将 Visual Studio Code 与 Python 一起使用时,我遇到了同样的问题。它与拥有另一个 pygame.py 或未正确安装它无关。它与 Visual Studio Code 从字面上理解您的代码这一事实有关,并且由于您无法 import pygame.init()
,它认为它不是一个正确的模块。
To fix this, open up settings.json (go into your settings, and click the {}
icon) and paste
要解决此问题,请打开 settings.json(进入您的设置,然后单击{}
图标)并粘贴
"python.linting.pylintArgs": [
"--extension-pkg-whitelist=pygame"
]
to it.
到它。
回答by Nearoo
Thisanswer includes the answer to your question. In short it explains:
此答案包括对您问题的答案。简而言之,它解释了:
Pylint imports modules to effectively identify valid methods and attributes. It was decided that importing c extensions that are not part of the python stdlib is a security risk and could introduce malicious code.
Pylint 导入模块以有效识别有效的方法和属性。决定导入不属于 python stdlib 一部分的 c 扩展是一种安全风险,可能会引入恶意代码。
and as a solution it mentions, among others:
作为一个解决方案,它提到,其中包括:
Disable safety using the
.pylintrc
settingunsafe-load-any-extensions=yes
.
使用
.pylintrc
设置禁用安全unsafe-load-any-extensions=yes
。
See herefor more information about pylint.rc
. Quickest method is to just create the file .pylintrc
in your project directory or your home directory.
有关更多信息,请参见此处pylint.rc
。最快的方法是.pylintrc
在您的项目目录或主目录中创建文件。
回答by Billy
I find an answer and it really works for me.
See the accepted answer and change it to extension-pkg-whitelist=lxml
我找到了一个答案,它真的对我有用。查看接受的答案并将其更改为extension-pkg-whitelist=lxml
回答by manoIk
I found adding this in settings.json() solves the problem.
我发现在 settings.json() 中添加它可以解决这个问题。
"python.linting.pylintArgs":[
"--extension-pkg-whitelist=pygame",
"--erros-only"
]
回答by banano
I had the same issue with one of my modules. This is what I did to resolve the problem. (I'm using visual studio on windows 10)
我的一个模块遇到了同样的问题。这就是我为解决问题所做的工作。(我在 Windows 10 上使用 Visual Studio)
- Press CTRL+SHIFT+Pin visual studio
- Choose "Preferences: Open Settings (JSON)"
- Add
"python.linting.pylintArgs": ["--generate-members"]
below one of the lines (put a comma if necessary) - Save the
.json
file (CTRL+S)
- 按CTRL+ SHIFT+P在Visual Studio
- 选择“首选项:打开设置(JSON)”
"python.linting.pylintArgs": ["--generate-members"]
在其中一行下方添加(如有必要,请使用逗号)- 保存
.json
文件 ( CTRL+ S)
For me, the code looks like this :
对我来说,代码如下所示:
{
"breadcrumbs.enabled": false,
"editor.minimap.enabled": false,
"python.pythonPath": "C:\Users\xxx\Anaconda3",
"terminal.integrated.rendererType": "dom",
"window.menuBarVisibility": "default",
"workbench.activityBar.visible": false,
"workbench.statusBar.visible": true,
"python.linting.pylintArgs": ["--generate-members"], //line to add
"[json]": {
}
}
Hope it helps. Credit to @Alamnoor on github
希望能帮助到你。归功于 github 上的@Alamnoor
回答by Akshat2001
Disable Pylint 1.Press ctrl + shift + p 2.Then type Disable Pylint
禁用 Pylint 1.按 ctrl + shift + p 2.然后输入 Disable Pylint
回答by Sanketh B. K
Check if you have a python file named pygame.py
created by you in your directory. If you do, then the import pygame
line is importing your own file instead of the real Pygame module. Since you don't have an init()
function in that file, you're seeing this particular error message.
检查pygame.py
您的目录中是否有由您创建的名为 python 的文件。如果您这样做了,那么该import pygame
行将导入您自己的文件而不是真正的 Pygame 模块。由于init()
该文件中没有函数,因此您会看到此特定错误消息。
回答by Epic Coder
If you are using vscode
then you can go to settings:
如果您正在使用,vscode
则可以转到设置:
python.linting.pylintEnabled = False
It will fix the problem. If you aren't using vscode
then you can go the command prompt and manually uninstall pylint
with the command
它将解决问题。如果您不使用,vscode
则可以转到命令提示符并pylint
使用命令手动卸载
pip uninstall pylint.