44
votes

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:

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

13
did you install pygame with pip or how did u install it? what happens when you run print(dir(pygame)) after your import? - MooingRawr
Also, what is printed with import pygame; print(pygame)? - zvone
I first installed pygame with pip but I also tried to download a python wheel file for it, extract it and place the pygame folder into the Lib\site-packages, which is where other stuff like cx_Freeze and Django is. Both methods yield the same result. - Ludde
print(dir(pygame)) gives me a large list of what looks like commands. among them, I can actually find 'init'. print(pygame) gives me this: <module 'pygame' from 'D:\\coding\\python\\lib\\site-packages\\pygame\_init_.py'> - Ludde
The code works, but only pylint tells you there is a problem, right? - MegaIng

13 Answers

29
votes

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.

28
votes

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
    ]
    
  2. You can allow to "unsafe load" all extensions.

    Open user settings and add the following between {}:

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

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=pygame"
]

to it.

16
votes

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)

  1. Press CTRL+SHIFT+P in visual studio
  2. Choose "Preferences: Open Settings (JSON)"
  3. Add "python.linting.pylintArgs": ["--generate-members"] below one of the lines (put a comma if necessary)
  4. Save the .json file (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

6
votes

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
votes

I found adding this in settings.json() solves the problem.

"python.linting.pylintArgs":[
            "--extension-pkg-whitelist=pygame",
            "--erros-only"
      ]
2
votes

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

2
votes

I recommend going to the view tab, clicking command palette and searching preferences: open settings.json. Then add a comma on the last line of code.Below that paste this:

"python.linting.pylintArgs": [
    "--extension-pkg-whitelist=extensionname" // comma separated
]

Then save your document (ctrl + s).

1
votes

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.

-1
votes

I found a solution, modifying the most voted answer:

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

Replaced the "lxml" with "pygame".

-3
votes

Disable Pylint 1.Press ctrl + shift + p 2.Then type Disable Pylint

-5
votes

I had the same problem while doing it in Vscode but it somehow resolved when i ran the python file from pc terminal . not the terminal in Vscode

-6
votes

If you are using vscode then you can go to settings:

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

pip uninstall pylint.