4

I use venv, and can sucessfuly run "py .\main.py" in the terminal window. The program uses packages from .venv\Lib\site-packages

when I 'run and debug 'with this launch.json below, I get a error: No module named 'simplipy'

Any tips? Thanks, Peter

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "Python: Current File",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal",
        "env": { "PYTHONPATH": "${workspaceRoot}"}
    }
]

}

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
petercli
  • 185
  • 2
  • 12
  • When your module can't be found, your environment hasn't been activated. – Peter Aug 31 '21 at 16:30
  • I agree . The wiki talks about environments located in the folder identified by the python.venvPath setting (see General settings), which can contain multiple virtual environments. The extension looks for virtual environments in the first-level subfolders of venvPath. – petercli Sep 01 '21 at 12:46
  • 2
    I got debugging to work. This article helped : https://stackoverflow.com/questions/58433333/auto-activate-virtual-environment-in-visual-studio-code – petercli Sep 01 '21 at 16:45

2 Answers2

0

Another solution is to load the module by path:

import importlib
import importlib.machinery
import importlib.util
import pathlib

path = s.path.abspath(os.path.join(pathlib.Path(__file__).parent.absolute(), 'myFile.py'))
loader = importlib.machinery.SourceFileLoader('myFile', path)
spec = importlib.util.spec_from_loader('myFile', loader)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

myClass = module.myClass()

Now debugging works fine, with correctly loaded my module.

FrankyHollywood
  • 1,497
  • 19
  • 18
  • 2
    this works, but I also posted an answer on how to setup VSCode launch.json correct see https://stackoverflow.com/a/73670198/669518 – FrankyHollywood Oct 12 '22 at 13:14
0

I'm just duplicating my answer to this related question here (https://stackoverflow.com/a/75658649/10881599) for anyone else who stumbles across this post in the future:

I had to change my launch.json to the following to be able to successfully debug any arbitrary python module I'd written in my project (by hitting F5 to start debugging with my .py file as VSCode's active file). Otherwise, I'd run into the same "ModuleNotFoundError" when the file tried to import from a different custom module.
OS = Ubuntu 20.04 (WSL2)

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "debug-my-code",
            "type": "python",
            "python": "/home/<user>/<path/to/my/repo>/.venv/bin/python",
            "request": "launch",
            "program": "${relativeFileDirname}/${fileBasename}",
            "purpose": ["debug-test"],
            "console": "integratedTerminal",
            "justMyCode": true,
            "env": {"PYTHONPATH": "/home/<user>/<path/to/my/repo>"},
        }
    ]
}

Notes

  • I had to put the full absolute path to my venv python as the python path - even using ~ in place of /home/user/ was rejected
  • similarly, I had to put the full absolute path to my repo in the PYTHONPATH environment variable
  • For the program I needed to specify the relative path to the file I was debugging, relative to the repo root.

E.g. if the file I'm trying to debug is <repo-root>/src/data/process.py, then "${relativeFileDirname}" gets me src/data, while "${fileBasename}" adds on the specific module process.py

Hope this helps someone. I tried many other combinations but this was the only one to finally work.

capohugo
  • 111
  • 1
  • 2