Why does it say that module pygame has no init member?

人盡茶涼 提交于 2019-12-09 07:59:01

问题


Following 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:

E1101:Module 'pygame' has no 'init' member

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. 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.

As said earlier, it seems like I'm able to run the file without any issues, but I am new to to this, 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.


回答1:


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

"python.linting.pylintArgs": [
    "--extension-pkg-whitelist=lxml"  // The extension is "lxml" not "1xml"
]

I no longer see the pyinit error.




回答2:


Summarizing all answers. This is a security measure to not load non-default C extensions.

  1. You can white-list specific extension(s).

Open user settings and add the following between {}:

"python.linting.pylintArgs": [
    "--extension-pkg-whitelist=extensionname" // comma separated
]
  1. You can allow to "unsafe load" all extensions.

Open user settings and add the following between {}:

"python.linting.pylintArgs": [
    "--unsafe-load-any-extension=y"
]

Thank you Richard and Nearoo.




回答3:


I find an answer and it really works for me. See the accepted answer and change it to extension-pkg-whitelist=lxml

pylint 1.4 reports E1101(no-member) on all C extensions




回答4:


This answer 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.

and as a solution it mentions, among others:

Disable safety using the .pylintrc setting unsafe-load-any-extensions=yes.

See here for more information about pylint.rc. Quickest method is to just create the file .pylintrc in your project directory or your home directory.




回答5:


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. To fix this, open up settings.json (go into your settings, and click the {} icon) and paste "python.linting.pylintArgs": [ "----extension-pkg-whitelist=1xml" ] to it.




回答6:


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.




回答7:


I found a solution, modifying the most voted answer:

"python.linting.pylintArgs": [
    "--extension-pkg-whitelist=pygame"
]

Replaced the "lxml" with "pygame".



来源:https://stackoverflow.com/questions/50569453/why-does-it-say-that-module-pygame-has-no-init-member

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!