14

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 file.py.

Unresolved reference

I have found these questions:

I have the following project structure:

Project structure

I have marked src as the sources root...

I have set the "Add source roots to PYTHONPATH":

Add sources option

I have tried File -> Invalidate Caches / Restart.. (I even restarted the computer).

If I try to run it, I get the following error in the console: ImportError: cannot import name 'MyClass'

The interpreter is a virtualenv on Python 3.4 on Ubuntu x64 14.04.

If I install and import any 3rd party packages, they work fine.

If I try echo $PYTHONPATH in the terminal it returns nothing (same with env | grep PYTHONPATH. I have the appropriate virtualenv active when I try these.

Any clues?

Community
  • 1
  • 1
Iulian
  • 1,496
  • 2
  • 15
  • 35

7 Answers7

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.

yole
  • 92,896
  • 20
  • 260
  • 197
5

If you are using python version 3 try this

from .pack import myclass

This worked for me

guntbert
  • 536
  • 6
  • 19
kayleb
  • 51
  • 1
  • 1
2

There are several reasons why this could be happening. Below are several steps that fixes the majority of those cases:

.idea caching issue

Some .idea issue causing the IDE to show error while the code still runs correctly. Solution:

  1. close the project and quick PyCharm
  2. delete the .idea folder where the project is. note that it is a hidden folder and you might not be aware of its existence in your project directory.
  3. start PyCharm and recreate the project

imports relative not to project folder

Relative imports while code root folder is not the same as the project folder. Solution:

  1. Find the folder that relative imports require in the project explorer
  2. right click and mark it as "Source Root"

Editor not marking init.py as Python

Which is the most illusive of all the cases. Here, for some reason, PyCharm considers all __init__.py files not to be python files, and thus ignores them during code analysis. To fix this:

  1. Open PyCharm settings
  2. Navigate to Editor -> File Types
  3. Find Python and add __init__.py to the list of python files or find Text and delete __init__.py from the list of text files

enter image description here

Ouss
  • 2,912
  • 2
  • 25
  • 45
  • This ".idea caching issue" was the clue that I needed. In PyCharm 2019.1 this is easily solved by selecting File >> "Invalidate Caches / Restart" – Lagrangian Aug 09 '23 at 00:23
1

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)
Dan
  • 151
  • 6
0

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

from .pack import MyClass

It will work

Paulo Victor
  • 3,814
  • 2
  • 26
  • 29
0

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.

Roo
  • 141
  • 2
  • 6
0

I just delete the copied the code and delete the file and again create the same, that time it will work

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 27 '22 at 14:44