1

I have an issue with importing modules from a subfolder in my projects main folder.

The project structure is:

project_folder/
|-- main.py
|
|-- tools/
    |-- script.py
|
|-- utils/
    |--__init__.py
    |--misc.py

When I try to import a function from the utils.misc module, I get a ModuleNotFoundError (using VSCode), but I can run the code with no issue when I use Spyder.

The statement I use in script.py to import is:

from utils.misc import my_function

How can I resolve this issue?

Please see a snapshot of the error below:

enter image description here

I have also added ${workpsaceFolder} to cwd configuration, and added the project root folder to the PYTHONPATH in the launch.json file (see below). However the project_folder still fails to show up when I print sys.path.

enter image description here

UPDATE: See the screenshot of the project structure and the error below

enter image description here

NIM4
  • 121
  • 1
  • 10

2 Answers2

1

Have you changed the cwd(official docs) configuration in the launch.json file? It defaults to ${workspaceFolder}. Then the import you take should work.

Update:

{
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal",
      "env": {
        "PYTHONPATH": "${workspaceFolder}"
      },
      "cwd": "${workspaceFolder}",
}

Add the env configuration.

Update

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

will not work when you use Code Runner.

Update: You need to run the code with terminal(F5) instead of Code Runner. Or like this:

enter image description here

And make sure save the file before you run the code.

UPDATE: See the screenshot of the project structure and the error below

enter image description here

NIM4
  • 121
  • 1
  • 10
Steven-MSFT
  • 7,438
  • 1
  • 5
  • 13
  • The `cwd` is set to the `${workspaceFolder}`, which is my project's root directory, but it's still unable to locate the module. – NIM4 May 27 '21 at 01:36
  • Can you navigate to misc in 'from utils.misc import my_function', with 'F12' or 'ctrl+click'? And can you run it successfully? If possible, can you provide a picture about it? – Steven-MSFT May 27 '21 at 03:08
  • It does run successfully. Seems like I'm not able to attach a photo in the comments. – NIM4 May 27 '21 at 04:01
  • Can you update the question to attach a photo? It looks like a problem with the linting. You should check what prompts the error. If it the language Server, try to switch to another one the same goes for the linter you are using. – Steven-MSFT May 27 '21 at 06:13
  • Does it work in the normal terminal instead of code runner? – Steven-MSFT May 27 '21 at 06:31
  • Added the `env` configuration as well, still not working unfortunately – NIM4 May 28 '21 at 00:04
  • Can you add 'print(sys.path)' to check the locations of the interpreter search for modules(https://docs.python.org/3/library/sys.html?highlight=sys%20path#sys.path)? Make sure the parent folder of 'utils' was in it. – Steven-MSFT May 28 '21 at 02:00
  • If the parent folder of 'utils' was not in it, add it to the 'PYTHONPATH' under the 'env' configuration. For example: ${workspaceFolder}/project_folder;${workspaceFolder} – Steven-MSFT May 28 '21 at 02:42
  • Thanks again for the help, I did what you suggested but still not getting it to run. Please see the edited question above to see the changes I've made. – NIM4 May 31 '21 at 06:11
  • Sorry for the late, could you have a look at the update? – Steven-MSFT May 31 '21 at 07:52
  • If you directly add 'import sys sys.path.append("g:\\projects\\project_folder")', before 'from utils.misc import my_function'. It works or not? – Steven-MSFT May 31 '21 at 09:56
  • it works when I add it manually to sys.path, but is there a way I can get around doing that? thanks again – NIM4 May 31 '21 at 11:12
  • Sorry for the late. How do you run it with terminal? F5? As you have configured the 'PYTHONPATH' under the 'env' configuration. You should get it with no problem.Please have a look at the update. – Steven-MSFT Jun 01 '21 at 01:31
  • running in terminal doesn't work unless I add the path manually to sys.path – NIM4 Jun 01 '21 at 01:38
  • Can you add ${workspaceFolder} in the PYTHONPATH? and provide the screenshot of the terminal? remember add ';' after project_folder. – Steven-MSFT Jun 01 '21 at 01:44
  • If you can provide a screenshot of the project struct, it will be great. – Steven-MSFT Jun 01 '21 at 01:46
  • Please see the update, added a screenshot in the question (also I added the screenshot to your answer by mistake, but can't delete it!) – NIM4 Jun 01 '21 at 08:30
  • You could not run it like this. You should run it through 'F5', otherwise, the configurations in the launch.json will not work. – Steven-MSFT Jun 01 '21 at 08:32
  • Thanks it works using F5, so it can only be run in debug mode? – NIM4 Jun 01 '21 at 08:55
  • Yes, it is. Otherwise, you need to configure the 'sys.path' in other ways, such as 'sys.path.append' we have talked about. – Steven-MSFT Jun 01 '21 at 09:06
0

Just add .env file in the root of your working directory (real_root_dir in my case)

  • Opened in VSCode folder is real_root_dir

  • Project tree is

real_root_dir
├── .env
├── other_project
│   └── c.py
└── project_root
    ├── a.py
    ├── b
    │   ├── __init__.py
    │   └── b.py
    └── run.py
  • content of .env file is
PYTHONPATH=project_root:other_project
  • run.py code
from a import A
from b.b import B
from c import C

print(A)
print(B)
print(C)

With this configuration VSCode will find even C and allow to jump into it, but python still don't know about C, so running output will be:

Traceback (most recent call last):
  File "/Users/sergei/temp/real_root_dir/project_root/run.py", line 3, in <module>
    from c import C
ModuleNotFoundError: No module named 'c'

Of course, importing of outside C module is not normal in usual cases.
But A and B will work properly with python interpreter and VSCode both.

rzlvmp
  • 7,512
  • 5
  • 16
  • 45
  • Could you clarify how I should add the `.env` configuration? Should I add it to the `launch.json` file or is there another way I should do it? – NIM4 May 27 '21 at 01:37
  • @NIM4 Sorry for lacks in explanation. `.env` is just a file. I updated project tree, so now you can check where you need to place it – rzlvmp May 27 '21 at 01:57
  • I attempted this and it's not working unfortunately. In `PYTHONPATH=project_root:other_project`, are `project_root` and `other_project` full directories? also what does the `:` represent? thanks for the help – NIM4 May 27 '21 at 04:11
  • `:` is a directory separator. https://stackoverflow.com/questions/39682688/how-to-set-pythonpath-to-multiple-folders `project_root` and `other_project` are relative paths that relate on `real_root_dir` – rzlvmp May 27 '21 at 04:43
  • Thanks, but it doesn't seem to solve the issue – NIM4 May 27 '21 at 06:22