Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

relative import in pycharm 2018 does not work like relative import in python 3.6

Tags:

I have read endless discussions on relative import in python, I think that one of the reasons it is so confusing is that it changes fro one Python version to another (my version is 3.6). But here the culprit seems to be PyCharm (unless i'm mistaken..) and i wonder if anyone came across a solution to this issue. for a project with this layout:

/project     |-- __init__.py     |---subfolder             |-- __init__.py             |-- AA.py             |-- BB.py 

Let's imagine that AA.py contains some fuction myfunc inside the file BB.py if i write this import:

from AA import myfunc 

Then python works perfectly, but PyCharm sees it as an error: enter image description here

So to make PyCharm happy i can add the . to the import and then the error is seemingly resolved:

from .AA import myfunc 

But then python is not happy, giving me the error: ModuleNotFoundError: No module named '__main__.AA'; '__main__' is not a package

So to conclude, I use the import that actually works (i.e. from AA import myfunc) but it would be great if i could make PyCharm agree to it somehow because then it offers features such as auto complete, go to definition and so on.

not duplicates: I know it seems like this subject was discussed over and over but it also has many aspects. Here i'm talking about the pycharm aspect and therefore this topic is new as far as i know.

  1. How does PyCharm handle relative imports of modules? - is a user who didn't add the root project dir to PYTHONPATH
  2. Pycharm auto relative imports - is talking about auto-import feature that is not the case here
  3. Subpackages and relative imports in PyCharm - is talking about import issue in python 2.7 but in here i'm not having any issue to import
  4. Relative imports for the billionth time - offeres a great review of importing issues and also features a very detailed answer - none of it helps in my case because i don't have any import issue. not to mention that it is python 2.7 topic and not 3.x
like image 961
Alon Avatar asked Aug 02 '18 08:08

Alon


People also ask

Why import is not working in PyCharm?

Troubleshooting: Try installing/importing a package from the system terminal (outside of PyCharm) using the same interpreter/environment. In case you are using a virtualenv/conda environment as your Project Interpreter in PyCharm, it is enough to activate that environment in the system terminal and then do the test.

How do you use relative import in Python?

Relative imports use dot(.) notation to specify a location. A single dot specifies that the module is in the current directory, two dots indicate that the module is in its parent directory of the current location and three dots indicate that it is in the grandparent directory and so on.

Should I use relative or absolute imports Python?

With your new skills, you can confidently import packages and modules from the Python standard library, third party packages, and your own local packages. Remember that you should generally opt for absolute imports over relative ones, unless the path is complex and would make the statement too long.


2 Answers

Mark subfolder as Source Root with right-click in the project tree -> Mark directory as ... -> Sources Root. PyCharm adds all Source Roots to PYTHONPATH by default so the issue should be resolved

The problem is PyCharm doesn't know you are going to execute BB.py directly, e.g. let's say you have main.py in the root with from subfolder import BB. Calling python main.py will raise ModuleNotFoundError: No module named 'AA' (make sure to use Python 3 to avoid implicit relative imports from Python 2).

Hope it makes sense and I didn't miss anything.

like image 124
Pavel Karateev Avatar answered Oct 05 '22 13:10

Pavel Karateev


You can get both python and pycharm to agree by using

from subfolder.AA import myfunc 

However, according to here, from .AA import myfunc appears to be the correct syntax. But idk why it's not working.

like image 20
Nipun Sampath Avatar answered Oct 05 '22 14:10

Nipun Sampath