PyCharm unresolved reference when importing class from other file

后端 未结 5 1210
遇见更好的自我
遇见更好的自我 2021-01-11 11:00

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

相关标签:
5条回答
  • 2021-01-11 11:22

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

    from pack.file import MyClass
    

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

    0 讨论(0)
  • 2021-01-11 11:32

    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)
    0 讨论(0)
  • 2021-01-11 11:38

    If you are using python version 3 try this

    from .pack import myclass
    

    This worked for me

    0 讨论(0)
  • 2021-01-11 11:44

    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.

    0 讨论(0)
  • 2021-01-11 11:49

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

    from .pack import MyClass
    

    It will work

    0 讨论(0)
提交回复
热议问题