0

I'm trying to work on a project with a structure as follows:

root_folder
├── __init__.py
├── a_folder
│   ├── __init__.py
│   └── script_1.py
├── b_folder
│   ├── __init__.py
│   └── script_2.py
└── script_3.py

I open the IDE (VS Code) on the root_folder. And I add the empty __init__.py file to both the sub-folder and the root_folder. I also save all and restart the IDE.

However, when I try to import the script_1 in script_2, the error appears:

# in script_2.py

from a_folder import script_1

ModuleNotFoundError: No module named 'script_1'

I also try many other ways, like

# in script_2.py

from root_folder.a_folder import script_1

# Or

from .a_folder import script_1

But they still don't work. I searched everywhere but still cannot solve it. Could you mind giving me some hints on it? Thank you!

Update

Here are some print results of the path:

# when I try to run script_2 in b_folder

print(os.path.abspath(''))
print(os.getcwd())

---/home/nick/Desktop/root_folder
---/home/nick/Desktop/root_folder


print(sys.path)

---
['/home/nick/Desktop/root_folder/b_folder', '/home/nick/anaconda3/envs/pytorch101/lib/python37.zip', '/home/nick/anaconda3/envs/pytorch101/lib/python3.7', '/home/nick/anaconda3/envs/pytorch101/lib/python3.7/lib-dynload', '/home/nick/anaconda3/envs/pytorch101/lib/python3.7/site-packages']

Nick Nick Nick
  • 159
  • 3
  • 13
  • `__init__.py` defines the folder as a module. `import script_1 from a_folder` – Marco Bonelli Oct 23 '20 at 03:22
  • I suspect you're hitting this: https://stackoverflow.com/a/45448394/812183 but I don't have enough confidence to VTC as duplicate – anthony sottile Oct 23 '20 at 03:32
  • Hi I don't think this is the duplicate one for two reasons: First, I'm trying to run a sibling script but not a parent one. Like if I try to run `script_3.py` then your link is the answer. Second is that ... both relative path and absolute path don't work here... But thank you a lot for your help anyway! – Nick Nick Nick Oct 23 '20 at 03:37

2 Answers2

2

Before the import sentence, add the following code

import sys
sys.path.append("./")

which adds the current path to workspace, then you can import module successfully.

Here's my project screenshot with the same folder structure as yours.

enter image description here

[ EDIT ]

I assumed your cwd(current working directory) is under the root_folder, like mine. then the sys.path.append("./") should work.

Now turn to the root_Project, add the following code in launch.json:

"env": {
    "PYTHONPATH": "${workspaceFolder}",
},
"cwd": "${workspaceFolder}"

Python extension will search module under root_project, you can directly import the module:

enter image description here

More detailed information please refer to env in debug and Variables.

Molly Wang-MSFT
  • 7,943
  • 2
  • 9
  • 22
  • Hi Molly! Thank you so much for your reply. I tried your method, however it doesn't work. Then I did a modification based on your suggestion: I use the `sys.path.append("./root_folder")`. It works now! – Nick Nick Nick Oct 23 '20 at 07:50
  • Actually I'm not sure why this happens, seems `./` and `./root_folder`, they are the same thing, right? Do you have any suggestions on it? Thank you. – Nick Nick Nick Oct 23 '20 at 07:52
  • I think your current working directory isn't \xxxx\root_folder which shown in Terminal. and that's why python extension can't achieve it. It's recommended turn to the current project folder and i'll edit my answer with the second choice, please have a look at it. – Molly Wang-MSFT Oct 23 '20 at 09:23
  • Thank you Molly! Now the relative path works! May I ask one more question? It seems we only need to use the `sys.append('./')` here, right? Because after deleted the `__init__.py`, it still imports perfectly with `sys.append`. Seems `__init__.py` cannot transfer the folder into the module properly in VS Code. – Nick Nick Nick Oct 23 '20 at 15:28
  • I also update the path env in the body of the question. Please feel free to check. Thank you! – Nick Nick Nick Oct 23 '20 at 16:11
  • Yes, without init.py, using sys.path.append("./") can also help import the module. But if you create a new project without __init__.py and choose the second way, which adds configuration in launch.json, the script1 won't be recognized as a module. You can have a look at official docs about __init__.py to get more detailed explanation: https://docs.python.org/3/reference/import.html#regular-packages. – Molly Wang-MSFT Oct 25 '20 at 13:37
0

Set your folder 'root_folder' as a 'Source Root' if you're still using IDE like Jetbrains Pycharm. If you're using VSC, the same operation you can do as the same. In the other way:

import sys
sys.path.append('your path')

can manually add the package to the ENV Path of system level.

Good luck.

Venti Fang
  • 11
  • 1
  • It works! Thank you Venti! May I ask: It seems `__init__.py` cannot transfer the folder into the module properly here, thereby let us skip this `sys.append` step, right? Because after I deleted all the `__init__.py`, it still imports perfectly with sys.append. Do you have any suggestions for it? Thank you! – Nick Nick Nick Oct 23 '20 at 15:32
  • I also update the path env in the body of the question. Please feel free to check. Thank you! – Nick Nick Nick Oct 23 '20 at 16:11