lorg
lorg

Reputation: 1170

How to make pycharm always recognize imports in the same directory?

I have a file a.py in a directory somewhere inside the project, and in the same directory I have b.py. Inside a.py I want to be able to do:

import b

This works in Python, but Pycharm doesn't recognize it. Is there a way to make Pycharm always recognize import relative to the directory of the importing file?

EDIT: to clarify the issue, I already have sources root defined, and a.py and b.py are not at the top level. Here is a screenshot showing the issue: enter image description here

EDIT 2: based on the comments, this question is invalid and I should not be importing code in the way described.

Upvotes: 1

Views: 294

Answers (3)

Gustavo Sousa
Gustavo Sousa

Reputation: 1039

You can do import .b.

Reference: https://docs.python.org/3/reference/import.html#package-relative-imports

Upvotes: 0

Right click on the folder that you're working on

Select "Mark directory as" > "Sources Root"

enter image description here

Upvotes: 1

shubham tripathi
shubham tripathi

Reputation: 51

Well this a feature with pycharm already, just mark the source root for your project and pycharm will take care of the rest

Upvotes: 0

Related Questions