Search code examples
pythonvisual-studio-codeimportpycharm

ModuleNotFoundError happening in Vscode but not in Pycharm


When I try to import my own modules in Vscode, I always get the ModuleNotFoundError. Meanwhile, when I do this in Pycharm, it works normally.

Both have the same folder structure

enter image description here

enter image description here

The code for lib.py is the same for both:

def hello():
    print("Hello World")

But when I import hello() to main.py, in Vscode, I get the error.


Solution

  • Very sorry for being late.

    This is because in the Pycharm it defaults selected Add content roots to PYTHONPATH(Edit Configurations). This means the parent folder path of main and lib folder will be added to the PYTHONPATH. While VSCode does not take it by default.

    You can add this to the settings.json file to achieve this in the VSCode:

      "terminal.integrated.env.windows": {
        "PYTHONPATH": "${workspaceFolder}"
      },