1
votes

I am trying to make pycharm to resolve reference to module when importing all modules from folder

screenshot

The code in the screenshot works fine but Pycharm cannot see businessnetworkcouk (or any other module in the folder projectsites) and complains about unresolved reference.

I've already tried

  • marking projectsites folder as source root
  • invalidate caches
  • restarting pycharm

If I import businessnetworkcouk module with

from projectsites import businessnetworkcouk

Pycharm recognises it well

2

2 Answers

0
votes

You can add the folder with your imports to your project structure. This way PyCharm will recognize your files.

It can be done under Preferences | Project:foo | Project Structure

0
votes

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