2

I am trying to resolve issue that pycharm shows me warnings:

Cannot find reference 'x' in 'y'

for things from pygame.

Example:

Cannot find reference 'circle' in 'draw.py'


In "Settings > Project Interpreter > Interpreter Paths" I have:

...
/usr/local/lib/python3.5/dist-packages/
/usr/local/lib/python3.5/dist-packages/pygame-1.9.2b8-py3.5-linux-x86_64.egg

And I have pygame in this location.

What can I do to get rid of those warnings?


Edit:

import pygame
import time

pygame.init()
resolution = (200, 200)
screen = pygame.display.set_mode(resolution)
background = pygame.Surface(screen.get_size())
background.fill((255, 255, 255))
background = background.convert()
pygame.draw.circle(background, (0, 0, 0), (100, 100), 50, 2)
screen.blit(background, (0, 0))
pygame.display.flip()
time.sleep(5)

This code will show a black circle on white background and it works when run by python3.

However I get these warnings in pycharm:

4: Cannot find reference 'init' in '__init__.py'
6: Cannot find reference 'set_mode' in 'display.py'
10: Cannot find reference 'circle' in 'draw.py'
12: Cannot find reference 'flip' in 'display.py'
Thun
  • 37
  • 5
  • My first guess would have been that your version of pygame is for Py2.7; but i guess from your local path it's alright - can you show us a (small) example of code? We might be able to help you better if we had more than the example to go with – Cribber Apr 12 '17 at 06:36
  • Example of code and list of warnings added to main post – Thun Apr 12 '17 at 07:56

1 Answers1

2

This is a non-critical "bug" in Pycharm. It has been around for quite a while and while I personally have not encountered it so far, it seems to be a feature rather than a bug.

You can remove this warning by adding the names of the modules (which are to be imported) in the __all__ variable (right after the import statement):

from . import pygame, time
__all__ = [pygame, time]

it might also only work with the names in quotes:

__all__ = ["pygame", "time"]

not sure right now.

Source: If you want to read up on it, here is a link to a similar problem with another module while running with Pycharm: Cannot find reference 'xxx' in __init__.py - Python / Pycharm

Edit: you can read up on how imports / the __all__ statement exactly work here (in case you are interested further): https://docs.python.org/3.4/tutorial/modules.html?#packages

Community
  • 1
  • 1
Cribber
  • 2,513
  • 2
  • 21
  • 60
  • I get error on import line: "This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items." – Thun Apr 12 '17 at 09:09
  • try importing them one by one, does it work that way? – Cribber Apr 12 '17 at 09:10
  • or import by: < import pygame, time> then you leave the "*" operator out (which is meant with the "from" prefix – Cribber Apr 12 '17 at 09:11
  • apparently its a temporary problem and will resolve itself; just found this link (look at answer 1): http://stackoverflow.com/questions/25147180/opening-attempting-to-read-a-file – Cribber Apr 12 '17 at 09:13
  • 1
    "Temporary" - this question is from 2014 O.o Looks like jetbrains doesn't care. Thanks for this link. – Thun Apr 12 '17 at 09:32
  • "temporary" as in it will go away after restarting pycharm / a couple of minutes / some other weird pycharm-ian measure :D no problem – Cribber Apr 12 '17 at 10:00