Home > Back-end >  Python in VS Code
Python in VS Code

Time:11-05

I'm trying to use Python 3.9 in VS Code. Consider following project folder structure:

C:\Project_folder\
                  app1\modules\some_module1.py
                       \some1.py
                  app2\modules\some_module2.py
                      \some2.py

If I tried to run the code of some2.py, in which:

import app1.modules.some_module1 as some_module2

**other python stuff**

It raise the error that app1 is uknown module. Even if,

"cwd": "${workspaceFolder}"

is in launch.json. When I tried to run:

import os
print(os.getcwd())
import app1.modules.some_module1 as some_module2

**other python stuff**

It returns me:

> C:\Project_folder

and raise the error of app1 module. What is wrong in this case? Why it can't see another directories?

CodePudding user response:

Are you debugging your python file? In that case, you should use the following in launch.json file:

"cwd": "${fileDirname}"

If you are running the python file using the run button in VS Code, then you need to set python.terminal.executeInFileDir to true in setting.json. You can do this by in settings in VS Code, by searching for python.terminal.executeInFileDir and set it to true, or simply paste this in the setting.json file

    "python.terminal.executeInFileDir": true,
  • Related