0

Here is my file structure...

my_project/
├── app1/
│   ├── __init__.py
│   └── module.py [class Tokens]
└── app2/
    ├── __init__.py
    └── feature1/
        ├── __init__.py
        └── script.py

In script.py, I have to import Tokens...

What I tried is: from app1.module import Tokens also from my_project.app1.module import Tokens

I am getting: ModuleNotFoundError: No module named 'app1' or 'my_porject'

Why am I getting this error and how do I resolve it.

jiisanda
  • 11
  • 1
  • 4
  • Can you check your cwd (current working directory)? There are many ways, but you can try `import os;print(os.getcwd())`. If the cwd is the (absolute) path of the `my_project` folder, let us know. – Anony Mous Apr 17 '23 at 11:14
  • Yes it is the absolute path... – jiisanda Apr 17 '23 at 11:18
  • Just to confirm, you used `from my_project...` not `form my_project...` (I think you did use `from`?) and that the `ModuleNotFoundError` you listed is from the second import statement? Also can you show me the output of `import os;print....`? – Anony Mous Apr 17 '23 at 11:25
  • Sorry that was typing error in question... No I am getting it from both the import statements and the output is ```C:\Users\Harsh\VSCode\tdc\The-Project\my_project``` – jiisanda Apr 17 '23 at 11:33
  • Ok I think I have a solution, give me a sec. – Anony Mous Apr 17 '23 at 11:41

1 Answers1

-1

You can try this:

import sys
sys.path.append(r'C:\Users\Harsh\VSCode\tdc\The-Project\my_project')
from module import Token

Let me know if this works!

Reference: Importing files from different folder

Anony Mous
  • 345
  • 3
  • 10
  • This is both not necessary and probably wrong. You shouldn't need to add to `sys.path` when importing from within a package and the path you append is relative to the running directory so who's to say there is even a `app1` directory there? – Tomerikoo Apr 17 '23 at 11:49
  • Now also getting the same error but for moule – jiisanda Apr 17 '23 at 11:53
  • Try replace `.\\app1` with the absolute path? I'll edit my answer – Anony Mous Apr 17 '23 at 11:56
  • @Tomerikoo I've edited the answer, and if you have a better answer, please make a suggestion. I'll be happy to remove my answer if your\ option is more appropriate. – Anony Mous Apr 17 '23 at 11:58
  • https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time here are 12 answers – Tomerikoo Apr 17 '23 at 12:12