Skip to content Skip to sidebar Skip to footer

Pycharm Unresolved Reference When Importing Class From Other File

This problem has been driving me nuts. I am trying to import a class from a file in the same directory. PyCharm is giving me the 'Unresolved reference' error. MyClass is defined in

Solution 1:

If MyClass is defined in pack/file.py, you need to import it as:

from pack.fileimportMyClass

Note that using names of Python built-in types (such as file) for your own modules is a bad idea.

Solution 2:

If you are using python version 3 try this

from .packimport myclass

This worked for me

Solution 3:

Yes, if you are using python 3 you should add something like this:

from .packimportMyClass

It will work

Solution 4:

The following steps solved my issues:

  • All directories required at least a blank __init__.py file
  • Mark all directories as source roots (per previous poster instructions)

Solution 5:

I had the same issue when I tried to import a new class, however I could successfully import functions from a file in the same directory. I still dont understand why I could not import my class but thought I would share the information for other users.

@kaylebs response worked for me. However I then added the src directory to the list of source directories, first link in @lulian 's question and could remove the '.' from my file name.

Post a Comment for "Pycharm Unresolved Reference When Importing Class From Other File"