48

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.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Ludde
  • 481
  • 1
  • 4
  • 3
  • 2
    did you install pygame with `pip` or how did u install it? what happens when you run `print(dir(pygame))` after your import? – MooingRawr May 28 '18 at 15:35
  • Also, what is printed with `import pygame; print(pygame)`? – zvone May 28 '18 at 16:20
  • 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 May 28 '18 at 18:26
  • `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: – Ludde May 28 '18 at 18:28
  • The code works, but only pylint tells you there is a problem, right? – MegaIng May 28 '18 at 18:49
  • @MooingRawr He doesn't have problems executing this program. It's just pylint telling him something is wrong, so pylint is the issue. (Or I misunderstood the question) – MegaIng May 28 '18 at 18:51
  • Yes I also think there is an issue with pylint. I don't get any errors when running the script. Only pylint gives me the error. It's just that I get confused when pylint constantly tells me I have these errors (that apparently don't exist?) – Ludde May 28 '18 at 19:32

12 Answers12

33

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"
    ]
    
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
vt.
  • 1,325
  • 12
  • 27
29

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.

HeatfanJohn
  • 7,143
  • 2
  • 35
  • 41
Parkofadown
  • 616
  • 7
  • 11
  • look in the link below. it gets rid of "Module 'pygame' has no 'init' member" error in vs code when you are trying to make a pygame. – Parkofadown Oct 22 '18 at 03:05
  • 7
    If you're using VS Code for Mac, this is what you need to do in order to edit the settings.json file: 1. Click on Code (i.e. the Visual Studio Code tab which is on the left of the 'File' tab) -> Preferences - > Settings 2. Scroll down to Extensions and click on Python in the list. 3. Click on any of the `Edit in settings.json` links. This opens up settings.json for editing. – Fritz Lim Feb 25 '19 at 12:43
  • 2
    Actually, I had to white-list `pygame` to get rid of these false errors. Should I edit this answer accordingly? Does white-listing `lxml` work for some users? – HeatfanJohn Jun 13 '19 at 20:27
  • 4
    It should be `"--extension-pkg-whitelist=pygame"`. – Gino Mempin Dec 24 '19 at 12:29
22

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 doesn't generate the proper linting for pygame, so it therefore thinks that pygame.init() isn't valid.

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

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

RobC
  • 22,977
  • 20
  • 73
  • 80
banano
  • 169
  • 1
  • 3
6

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.

Nearoo
  • 4,454
  • 3
  • 28
  • 39
5

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

"python.linting.pylintArgs":[
            "--extension-pkg-whitelist=pygame",
            "--erros-only"
      ]
manoIk
  • 61
  • 1
  • 4
  • 1
    Note that the `"--errors-only"` is not required. It looks like that disables pylint warnings, which I would say is a bad idea (pylint warnings will help you write better code - you should leave them on). – Michael Dorst Oct 31 '20 at 08:50
2

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

Billy
  • 701
  • 1
  • 8
  • 26
2

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

vlizana
  • 2,962
  • 1
  • 16
  • 26
1

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.

Al Sweigart
  • 11,566
  • 10
  • 64
  • 92
Sanketh B. K
  • 759
  • 8
  • 22
-1

I found a solution, modifying the most voted answer:

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

Replaced the "lxml" with "pygame".

-4

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

  • 3
    Maybe the OP doesn't want to disable pylint. – 10 Rep May 12 '20 at 18:30
  • While standards can be "annoying" sometimes... they are usually there to make us better programmers. You'd be better adding a pylint disable comment on the error (or just removing the specific `no-member` check) – Andrew Bowman Feb 23 '21 at 15:32
-7

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. 
Rafael Marques
  • 1,501
  • 15
  • 23