Vox
Vox

Reputation: 531

Pycharm import unresolved

It can work but... enter image description here

It is all fine until I create new python3.7.7 virtualenv

enter image description here

and I notice that init.py is different from before.

enter image description here

Upvotes: 0

Views: 36

Answers (2)

Vox
Vox

Reputation: 531

Oh...my fault...

File--->Settings--->Editor--->File Types ---> find Text ---> delete init.py

I really don't when and how I modified the config.

enter image description here

Upvotes: 1

Ouss
Ouss

Reputation: 3855

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 (screenshot) or find Text and delete init.py from the list of text files

enter image description here

Upvotes: 0

Related Questions