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:
- I am not declaring a different / special python version at the top of cool_script.py
- I made sure that the path to
my_module
is correct - I put
__init__.py
files (empty files) both inmy_module
and incur_proj
- I am not using
virtualenv
Addendum 2015-Feb-25
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:
- What are "content roots" and what are "source roots"? And why does adding something to the PYTHONPATH make it somehow break?
- should I uncheck both of those options all the time (so also in the defaults, not only the project specific configurations (left panel of the Run/Debug Configurations dialog)?
This can be caused when Python interpreter can't find your code. You have to mention explicitly to Python to find your code in this location.
To do so:
sys.path.extend(['your module location'])
to Python console.In your case:
On the start, write the following code:
Once you have written this statement you can run following command:
The answer that worked for me was indeed what OP mentions in his 2015 update: uncheck these two boxes in your Python run config:
I already had the run config set to use the proper venv, so PyCharm doing additional work to add things to the path was not necessary. Instead it was causing errors.