I have written a module (a file my_mod.py
file residing in the folder my_module
).
Currently, I am working in the file cool_script.py
that resides in the folder cur_proj
. I have opened the folder in PyCharm using File -- open (and I assume, hence, it is a PyCharm project).
In ProjectView (CMD-7), I can see my project cur_proj
(in red) and under "External Libraries" I do see my_module
. In cool_script.py, I can write
from my_module import my_mod as mm
and PyCharm even makes suggestion for my_mod. So far so good.
However, when I try to run cool_script.py, PyCharm tells me "No module named my_module"
This seems strange to me, because
A) in the terminal (OS 10.10.2), in python, I can import the module no problem -- there is a corresponding entry in the PYTHONPATH in .bashrc
B) in PyCharm -- Settings -- Project cur_proj -- Project Interpreter -- CogWheel next to python interpreter -- more -- show paths for selected interpreter icon, the paths from PYTHONPATH do appear (as I think they should)
Hence, why do I get the error when I try to run cool_script.py? -- What am I missing?
Notes:
my_module
is correct__init__.py
files (empty files) both in my_module
and in cur_proj
virtualenv
When I go in PyCharm to Run -- Edit Configurations, for my current project, there are two options that are selected with a check mark: "Add content roots to PYTHONPATH" and "Add source roots to PYTHONPATH". When I have both unchecked, I can load my module.
So it works now -- but why?
Further questions emerged:
If your own module is in the same path, you need mark the path as Sources Root
. In the project explorer, right-click on the directory that you want import. Then select Mark Directory As
and select Sources Root
.
I hope this helps.