0

My code structure is as follows:

Main Code
   Directory 1
      file1.py
      file2.py
      ...
   Directory 2
     x.py
     y.py
   Directory 3
     main_script.py

In main script.py we have the following imports

from MainCode.Directory2.x import *

When I execute this code,I get the error: No module named MainCode

Any ideas on how to fix these kind of errors.I am assuming that this knowledge will be useful as I develop bigger packages and I can't have all my files living in one directory.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • this will help https://stackoverflow.com/a/4383597/9050514 – deadshot Jul 13 '20 at 09:21
  • 1
    Does this answer your question? [Importing files from different folder](https://stackoverflow.com/questions/4383571/importing-files-from-different-folder) – deadshot Jul 13 '20 at 09:21
  • 1
    You need `__init__.py` in the folders you are importing from – Chase Jul 13 '20 at 09:21
  • tried https://stackoverflow.com/questions/4383571/importing-files-from-different-folder didn't help.@Chase how does __init__.py help ,does that help the compiler in distinguishing between relative and absolute imports? – Sparsh Garg Jul 13 '20 at 09:23
  • Actually,the actual directory structure is as follows.And yes every file has __init__.py file in it. DeepRL2019 CS285 Agents __init__.py base_agent.py bc_agent.py pg_agent.py Infrastructure logger.py,rl_trainer.py,tf_utils.py,utils.py,__init__.py Policies Example of import from cs285.infrastructure.utils import * – Sparsh Garg Jul 13 '20 at 09:24
  • did you tried giving a space between main and code as in your question line 2 – Divyessh Jul 13 '20 at 09:24
  • probably it is related to how you running your script from which directory, run it from the parent directory of Mainfolder – sahasrara62 Jul 13 '20 at 09:25
  • @SparshGarg did you tried all the answers in this https://stackoverflow.com/questions/4383571/importing-files-from-different-folder – deadshot Jul 13 '20 at 09:26
  • @deadshot yes I did,__init__.py,export ,import sys(and its variations) – Sparsh Garg Jul 13 '20 at 09:32

0 Answers0