0

I try to understand where is my problem. This is my project

└── test_folder
    |___ __init__.py
    ├── foled_1
    │   ├── __init__.py
    │   └── file_1.py
    └── foled_2
        ├── __init__.py
        ├── file_2.py

When I try to run the script of file_1.py from the PyCharm, everything works perfectly for me. But when I try to run this file via terminal (python3 file_1.py) I got the error:

Traceback (most recent call last):
  File "file_1.py", line 1, in <module>
    from foled_2.file_2 import a
ModuleNotFoundError: No module named 'foled_2'

The script of file_1.py is:

from foled_2.file_2 import a

print(a)
print("Hello")

The script of file_2.py is:

a = 2
andrew
  • 51
  • 7
  • I'd guess what pycharm does is running `python3 foled_1/file_1.py` from `test_folder` (instead of from `test_folder/foled_1`). – Imperishable Night Jun 26 '19 at 06:45
  • Possible duplicate of [Import a module from a relative path](https://stackoverflow.com/questions/279237/import-a-module-from-a-relative-path) – Dino Jun 26 '19 at 06:47

1 Answers1

0

if you run the file from test folder like python foled_1/file_1.py it will work.

why this is not working?

this is because in pycharm you have defined test_folder as base project folder.so whenever you run a script, it will run from that folder , ie it take termnial current path as that base folder.

also in your code you have mentioned that you need foled_2 , since you are in foled_1 path and running script there, python is unable to locate the foled_2 folder/package.

solutuion

so if you want to run your script, run it from testfolder in terminal. python foled_1/file_1.py

sahasrara62
  • 10,069
  • 3
  • 29
  • 44