0

I have read Use a python package in the same project folder in visual studio code but it still doesn't work. Environment: Windows 10, Python 3.10.1 virtual environment, VSCODE 1.60.2 VS Default Interpreter: C:\data\tools\python310\venv\Scripts\python.exe I have a python 3.9 installed, and installed all the modules for the 3.9 to C:\data\tools\Python. I have assigned the PYTHONPATH variable to at the system level for that 3.9. This is my project file structure:

.
│   .env
├───.vscode
│       launch.json
│       settings.json
├───api
│       adhoc.py
│       __init__.py
├───config
│       config.json
├───test
│   │   testUtil.py
├───utils
│   │   dbutil.py
│   │   fileutil.py
│   │   util.py
│   │   __init__.py

Here's how I start my VSCODE from Windows Command Line:

set PYTHONPATH=
cd C:\data\tools\python310\venv\Scripts
activate
"C:\Program Files\Microsoft VS Code\Code.exe"

Here's my .env:

PYTHONPATH=.\utils:${PYTHONPATH}

Here's my settings.json:

{
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "./test",
        "-p",
        "*test*.py"
    ],
    "python.envFile":${workspaceFolder}/.env
    "python.testing.pytestEnabled": false,
    "python.testing.unittestEnabled": true,
    "python.linting.enabled": true
}

Here's my adhoc.py:

from utils.dbutil import readSQL, run_select
from utils.util import get_time_file, get_path
  • I can run all the tests successfully from VSCODE->Discover Test etc
  • I can run the adhoc.py successfully from pycharm
  • When I right click adhoc.py in VSCODE->Run Python File from Terminal, the error message is as below:
ModuleNotFoundError: No module named 'utils'

Any idea please?

Ginger_Chacha
  • 317
  • 1
  • 11
  • Try adding the absolute path to the folder instead of the relative one. Also, try adding the project folder to your pythonpath instead of utils – vladko312 Feb 12 '22 at 21:08
  • Thanks @vladko312. I added the project path into my .bat file and it works. In addition I found the correct solution here: https://stackoverflow.com/questions/53653083/how-to-correctly-set-pythonpath-for-visual-studio-code. For terminal running additional configuration needs to be added into the settings.json – Ginger_Chacha Feb 13 '22 at 08:08

0 Answers0