PyCharm - How Pycharm manipulate import
I have the following directory hierarchy:
root_folder
|---- main.py
|---- folder_2
|---- my_module1.py
|---- folder_3
|---- my_module2.py
In main.py:
import folder_2.my_module1.py
In my_module1.py:
import folder_3.my_module2.py
If I run main.py, I will occur an error where my_module1 cannot find my_module2. This can be fixed by changing my_module1.py to the following:
import folder_2.folder_3.my_module2
My question is: Is there any way that I can achieve the initial attempt to import my_module2 in my_module1? Thanks.
请先登录再写评论。
The only manipulation that you can do from PyCharm side is to mark folder_2 as Sources Root (right-click -> Mark Directory as -> Sources Root).
Or you can do it for folder_3 and then use import my_module2 in my_module1.
Sergey Karpov Thanks. That definitely helps a bunch. I now have a similar problem, maybe you can provide some input.
I did what you have suggested initially (change folders to source folder). Now, let say on folder 2 I have a dll called "my_bin.dll".
If in my_module1.py I did the following:
It seems like PyCharm won't be able to find the library. But as soon as I move my_bin.dll to the root folder, it can find the file immediately. Is there any way to fix this? I'm just trying to organising my files here.
Do you mean that when you run the code it can't find it? If so, then it's not really PyCharm that can't find it, it's Python. Have you tried specifying the absolute path?
Sergey Karpov The absolute path will definitely work. But I would like to use the code on another computer, which might create a problem. It seems to me that if I move the dll file to the root_folder, PyCharm can automatically pick up the name, which leads me to believe that there is something about folder setting that can cause this behavior.