Home > Net >  VSCode python can't import from parent folder
VSCode python can't import from parent folder

Time:04-05

I'm using VSCode on Windows as my IDE and my project structure looks like this:

`project_folder`

|---- `folder1`

|     |---- `folder2`

|     |     |---- `foo.py`

|     |---- `bar.py`

      .  
      .
      .

In foo.py I have this import statement: from folder1.bar import SomeClass

I can directly run that statement using PyCharm, but when I run it with VSCode I get the following error: ModuleNotFoundError: No module named 'folder1'

I tried modifying .vscode/settings.json and .vscode/launch.json following some tutorials but it didn't work.

I don't want to modify the system path using sys.path.append as it doesn't sound practical to do for every single file in the repo.

CodePudding user response:

I just solved the problem with avoiding sys.path.append or relative imports. Just set the environment variable PYTHONPATH and set the value to the path to your project root.

  • Related