PyCharm error: 'No Module' when trying to import own module (python script)
Solution 1
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
.
Solution 2
So if you go to
-> Setting -> Project:My_project -> Project Structure,
Just the directory in which the source code is available and mark it as "Sources" (You can see it on the same window). The directory with source code should turn blue. Now u can import in modules residing in same directory.
Solution 3
PyCharm Community/Professional 2018.2.1
I was having this problem just now and I was able to solve it in sort of a similar way that @Beatriz Fonseca and @Julie pointed out.
If you go to File
-> Settings
-> Project: YourProjectName
-> Project Structure
, you'll have a directory layout of the project you're currently working in. You'll have to go through your directories and label them as being either the Source
directory for all your Source files, or as a Resource
folder for files that are strictly for importing.
You'll also want to make sure that you place __init__.py
files within your resource directories, or really anywhere that you want to import from, and it'll work perfectly fine.
Solution 4
What I tried is to source the location where my files are.
e.g. E:\git_projects\My_project\__init__.py is my location.
I went to File -> Setting -> Project:My_project -> Project Structure and added the content root to about mention place E:\git_projects\My_project
it worked for me.
Solution 5
Always mark as source root the directory ABOVE the import!
So if the structure is
parent_folder/src/module.py
you must put something like:
from src.module import function_inside_module
and have parent_folder
marked as "source folder" in PyCharm
Related videos on Youtube
Claus
geostatistics, statistics, scientific programming, hydrology, hydrogeology, environmental engineering
Updated on July 17, 2022Comments
-
Claus almost 2 years
I have written a module (a file
my_mod.py
file residing in the foldermy_module
). Currently, I am working in the filecool_script.py
that resides in the foldercur_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 seemy_module
. In cool_script.py, I can writefrom 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)?
-
ziddarth over 9 yearsYou may have already tried this but when importing modules from other packages, include packagename
from foldername.mymodule import mymethod
. Another thing I've had to do is to add the empty _init.py folder in all the folders that you would use to get to your module so in case offrom parentfolder.childfolder.mymodule ...
you would need init in two folders. Depending on where the folders are located relative to each other you might need to have a top level package insys.path
as described in this answer -
user3155053 over 8 yearsI had your problem too. The following post solved my issues: stackoverflow.com/questions/21236824/…
-
Nithish Inpursuit Ofhappiness almost 7 yearsDid you identify why unchecking those checkboxes actually works as opposed to checking them?
-
Eric Blum over 6 yearsWhat worked for me is unchecking
Enable Django Support
in the settingsLanguages & Frameworks -> Django
in the Pro version of PyCharm. It was opening a django console which caused the import issues. -
user2939212 about 2 yearsIf you are coming here with a locally developed library and importing a newly added module running
pip install -e .
again in the terminal solved my issue.
-
grinch almost 8 yearsThis worked for me but I also had to delete and re-create the run configuration that I had previously created before marking the folder as sources root.
-
Beatriz Fonseca almost 8 yearsYou can create a init.py in a folder, python interpreter will read as python package: mikegrouchy.com/blog/2012/05/be-pythonic-__init__py.html
-
Beatriz Fonseca about 7 yearsAnd need add
__init__.py
to make Python treat the directories as containing packages: stackoverflow.com/questions/448271/… -
Boudewijn Aasman about 7 yearsHow would you do this if you weren't using pycharm? I'm running into the exact same problem.
-
Boudewijn Aasman about 7 years@BeatrizFonseca Yes I did. I couldn't get it to work in sublime, but I installed pycharm and tried the answer here and it worked perfectly fine, so it must have something to do with specifying the right directory as the root package.
-
Beatriz Fonseca about 7 years@BoudewijnAasman you could try use
.
before package name to specify that this package is local one -
Microos over 6 yearsSource the folder in the Project Structure worked for me. Thx.
-
Florian Blume almost 6 yearsA bit ugly to add the path like that but it worked for me.
-
Astitva Srivastava almost 6 yearsThis is not specific to PyCharm Professional, rather this feature is available in PyCharm Community edition as well. In the Project Structure, right-click the directory containing the module then select
Mark Directory as
from the context menu and selectSources Root
. -
Josh Friedlander about 5 yearsAmazingly, this was what I needed, despite doing
Invalidate Cache/Restart
twice! -
KLaz almost 5 yearsThanks @Virendra Patel and @Microos. My pycharm project is in
/home/my_user/git_projects/this_git_project/this_pycharm_project/
. I added/home/my_user/git_projects/this_git_project/
as content root and/home/my_user/git_projects/this_git_project/this_pycharm_project/
as the only source folder and everything worked out finally. -
user3341078 almost 5 yearsCan you please advise what is meaning of this Sources Root. Though it solved my problem but didn't quite understand why i am doing it.
-
sam over 4 yearsWhy is it necessary if the module is in the same path? If my module is in a sub-directory and I mark the parent directory as Sources Root, I'd think it can find module?
-
fabiob over 4 yearsthis should be done by pycharm, automatically when opening a console he should include sys.path.extend commands for the directories including dependencies (other projects) and the directories containing source code of the present project. BUT IT DOESN'T do it automatically
-
jaggi over 4 years@BeatrizFonseca What does marking folder as
Sources Root
do internally in Pycharm? does it add the directory path toPYTHONPATH
? -
Beatriz Fonseca over 4 yearsNo, in the PyCharm docs, Source Root is used to resolve imports. I do not know if the path is added to
PYTHONPATH
jetbrains.com/help/pycharm/content-root.html#root_types -
Bn.F76 almost 4 years@jaggi it adds the path to
PYTHONPATH
which you can check withprint(os.environ['PYTHONPATH'])
. Initially you'll have something like /Users/user/PycharmProjects/test_stuff. And after you mark the folderSources Root
you'll see something like /Users/user/PycharmProjects/test_stuff:/Users/user/PycharmProjects/test_stuff/package_1 -
Stefano almost 3 yearsWeird.. I marked the equivalent of
src
as source folder but then using yoursrc.module
solved it for me! Thanks, although I am still confused