PyCharm error: 'No Module' when trying to import own module (python script)

240,883

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

Share:
240,883

Related videos on Youtube

Claus
Author by

Claus

geostatistics, statistics, scientific programming, hydrology, hydrogeology, environmental engineering

Updated on July 17, 2022

Comments

  • Claus
    Claus almost 2 years

    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:

    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
      ziddarth over 9 years
      You 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 of from 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 in sys.path as described in this answer
    • user3155053
      user3155053 over 8 years
      I had your problem too. The following post solved my issues: stackoverflow.com/questions/21236824/…
    • Nithish Inpursuit Ofhappiness
      Nithish Inpursuit Ofhappiness almost 7 years
      Did you identify why unchecking those checkboxes actually works as opposed to checking them?
    • Eric Blum
      Eric Blum over 6 years
      What worked for me is unchecking Enable Django Support in the settings Languages & Frameworks -> Django in the Pro version of PyCharm. It was opening a django console which caused the import issues.
    • user2939212
      user2939212 about 2 years
      If 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
    grinch almost 8 years
    This 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
    Beatriz Fonseca almost 8 years
    You 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
    Beatriz Fonseca about 7 years
    And need add __init__.py to make Python treat the directories as containing packages: stackoverflow.com/questions/448271/…
  • Boudewijn Aasman
    Boudewijn Aasman about 7 years
    How would you do this if you weren't using pycharm? I'm running into the exact same problem.
  • Boudewijn Aasman
    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
    Beatriz Fonseca about 7 years
    @BoudewijnAasman you could try use . before package name to specify that this package is local one
  • Microos
    Microos over 6 years
    Source the folder in the Project Structure worked for me. Thx.
  • Florian Blume
    Florian Blume almost 6 years
    A bit ugly to add the path like that but it worked for me.
  • Astitva Srivastava
    Astitva Srivastava almost 6 years
    This 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 select Sources Root.
  • Josh Friedlander
    Josh Friedlander about 5 years
    Amazingly, this was what I needed, despite doing Invalidate Cache/Restart twice!
  • KLaz
    KLaz almost 5 years
    Thanks @Virendra Patel and @Microos. My pycharm project is in /home/my_user/git_projects/this_git_project/this_pycharm_pro‌​ject/. I added /home/my_user/git_projects/this_git_project/ as content root and /home/my_user/git_projects/this_git_project/this_pycharm_pro‌​ject/ as the only source folder and everything worked out finally.
  • user3341078
    user3341078 almost 5 years
    Can 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
    sam over 4 years
    Why 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
    fabiob over 4 years
    this 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
    jaggi over 4 years
    @BeatrizFonseca What does marking folder as Sources Root do internally in Pycharm? does it add the directory path to PYTHONPATH ?
  • Beatriz Fonseca
    Beatriz Fonseca over 4 years
    No, 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
    Bn.F76 almost 4 years
    @jaggi it adds the path to PYTHONPATH which you can check with print(os.environ['PYTHONPATH']). Initially you'll have something like /Users/user/PycharmProjects/test_stuff. And after you mark the folder Sources Root you'll see something like /Users/user/PycharmProjects/test_stuff:/Users/user/PycharmPr‌​ojects/test_stuff/pa‌​ckage_1
  • Stefano
    Stefano almost 3 years
    Weird.. I marked the equivalent of src as source folder but then using your src.module solved it for me! Thanks, although I am still confused