Include content/project root as an import starter

Hi!

Currently I've faced a problem working with imports in two project, where one is submodule of the other.
The situation is as follows:
- There is a repository, e.g "core", which contains shared code for other project which are built on top of it
- There is a project repository, e.g "project", which pulls "core" as a submodule

"core" structure:

core/
|__ code/
| |__ file1.py
|__ tests/

"project" structure:

project/
|__ core/ <-- (git submodule reference)
|__ project1/
|__ code/
| |__ file2.py
|__ tests/

 

The "project1" imports are ok, I can do them like

from core.code.file1 import SomeClass
from project.code.file2 import SomeClass2

But when I'm working with core repository alone, PyCharm can't detect import of type

from core.code.file1 import SomeClass

Thus, I'm forced to use imports like

from code.file1 import SomeClass
# But want to use like below
# from core.code.file1 import SomeClass

It works fine for most cases, but this introduces a problem of double imports, where by importing same class from different modules we are left with two different instances, e.g two instances in this case will be
`<core.code.file1.SomeClass>` and `<code.file1.SomeClass>`.
To solve this problem, I want to make all imports in "core" repository to start with `core.`

So, my main question how do I make imports and autocompletion work in PyCharm IDE to be able to use `core` prefix inside "core" repository imports? Picture of desired outcome (to be without errors):

I've already tried to set PYTHONPATH in various preferences parts (this made code actually run through python console interpreter) and tried to set core as a content root, but it didn't help.

For example, in VSCode such config will make import completion work for core repository:

"python.analysis.extraPaths": [
  ".."
]

Would be glad to hear any other way to deal with above mentioned problem!

4 comments
Comment actions Permalink

Hi Dmitry,

I order to be able to import modules starting from "core" you need its containing directory to be either a content root or a source root (see the tree in Settings | Project: ... | Project Structure). The reason is we can't be sure that you want to work only with symbols from the "core" package itself and not with its siblings under "project" so we need to have the whole content of "project" scanned, indexed and ready for that.

On a side note, why don't you use relative imports inside "core" so that you don't need to mention "core" in import paths, e.g. `from .internal.code.abstract... import ...`?

0
Comment actions Permalink

Thanks for the answer!

Ok, I got it

Relative imports are not an option, because core has a deep hierarchy, e.g

if I have class -> core.internal.code.mod_a.mod_b.Class
it can have an import of type

from core.internal.code.abstract import Class1 

the relative import will be resolved to path

from core.internal.code.mod_a.mod_b.internal.code.abstract import Class1

which obviously does not exist

0
Comment actions Permalink

I have exactly the same issue, but my source root is set. Still Pycharm is "unresolved reference 'api", where in my case it is "api" instead of "core" from OP.

0
Comment actions Permalink

Edumucelli Hi, feel free to provide a project sample to reproduce the issue, I will try to help.

0

Please sign in to leave a comment.