Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with Intelij IDEA importing classes, "cannot find reference" but code still works - Python

I am having problems with Intelij IDEA (community edition 2019.3.3 with the latest updates). When I ty to import a class from a different .py file that I have made, it is red underlined and says "cannot find reference". The code still works fine but it is really annoying it doesnt give me options when I start typing .lives or .name etc

I have tried invalidate caches / restart but after it finishes indexing the problem is back, how could I sort this?

enter image description here

folder hierarchy screen shot

class Player:
def __init__(self, name):
    self.name = name
    self._lives = 3
    self._level = 1
    self._score = 0

Main

from player import Player

kev = Player("Kevin")
print(kev.name)
like image 529
Kevin Nisbet Avatar asked Dec 06 '25 05:12

Kevin Nisbet


1 Answers

Solution: mark the 'Game' directory as a sources root (right-click on the folder > Mark Directory as > Sources Root). It should be colored blue.

More on content roots in IDEA: https://jetbrains.com/help/idea/content-roots.html

like image 146
Koyasha Avatar answered Dec 07 '25 20:12

Koyasha