Robert O'Brien
Robert O'Brien

Reputation: 183

ModuleNotFoundError: No module named 'app' when trying absolute imports

I have been trying to import files into my main.py file by using imports from the sources root. Here is a pic of my file structure...

file_structure_screencap

I am simply using source root imports to try and import seed.py into my main.py file. but keep getting this error message.

**ModuleNotFoundError: No module named 'app'**

This makes no sense to me as my app is a module and has its own init file. Im not too sure If i have something incorrect in my file structure or if it is just pycharm being buggy. But i dont see a reason why this import shouldnt work.

here is a code snippet of the import statement I am using

from app.bitcoinBackend.seed import *

any ideas??

Upvotes: 1

Views: 2175

Answers (2)

Robert O'Brien
Robert O'Brien

Reputation: 183

Okays so the solution i found involved moving main.py out of the frontend module back into the app directory. Here is an image of the full restructure.

enter image description here

next you had to select a directory as sources root I haven't looked into it too much, but when I correctly selected the sources root, all sub dirs changed into having the appropriate module symbol. (you can see this is app and bitcoinBackend)

I selected the teamSoftwareProject as the sources root. There are two ways of doing this one way involves

  1. right clicking the directory you want to set as sources root
  2. selecting Mark Directory As
  3. and then choosing sources root

Here is an image

sources root

The other method is

  1. Selecting file -> Settings
  2. Choose project structure
  3. Then select your sources root folder
  4. Click Apply and then Ok

Here is an image of this method. sourcesrootmethod2

Finally I added init.py files to all other sub dirs

Then importing from the project root worked.

from app.bitcoinBackend.seed import *

Upvotes: 0

Maybe you should try move your main.py to the root folder of app and just call bitcoinBackend.seed import *. Because your main is in a subfolder of app.

Upvotes: 1

Related Questions