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 worked for me: Go to Run-> Edit Configuration-> Python -> FileName.py Under working directory, checl :Add source roots to PYTHONPATH
my_module
is a folder not a module and you can't import a folder, try movingmy_mod.py
to the same folder as thecool_script.py
and then doimport my_mod as mm
. This is because python only looks in the current directory andsys.path
, and so wont findmy_mod.py
unless it's in the same directoryOr you can look here for an answer telling you how to import from other directories.
As to your other questions, I do not know as I do not use PyCharm.
The solution for this problem without having to Mark Directory as Source Root is to Edit Run Configurations and in Execution select the option "Redirect input from" and choose script you want to run. This works because it is then treated as if the script was run interactively in this directory. However Python will still mark the module name with an error "no module named x":
When the interpreter executes the import statement, it searches for x.py in a list of directories assembled from the following sources:
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 selectMark Directory As
and selectSources Root
.I hope this helps.
The key confusing step that must be done is to recreate the run configuration for the source file that you're trying to execute, so that the IDE picks up the new paths.
The way that actually worked for me was to go to Run/Edit Configurations..., select the configuration for the file that you're trying to run on the left side, uncheck the "Add source roots to PYTHONPATH" box, save, and then go back and check the box and save. THEN it would work.
I was getting the error with "Add source roots to PYTHONPATH" as well. My problem was that I had two folders with the same name, like
project/subproject1/thing/src
andproject/subproject2/thing/src
and I had both of them marked as source root. When I renamed one of the"thing"
folders to"thing1"
(any unique name), it worked.Maybe if PyCharm automatically adds selected source roots, it doesn't use the full path and hence mixes up folders with the same name.