pyflow icon indicating copy to clipboard operation
pyflow copied to clipboard

Debug python in vscode

Open quycao opened this issue 4 years ago • 1 comments

I can only run python code with pyflow, there is no instruction how to debug with it. I used vscode and it don't recognize the .venv of pyflow. Hope you can add debug feature to pyflow.

quycao avatar Dec 16 '20 16:12 quycao

@quycao I had a similar problem and solved it reading the vscode documentation + Stackoverflow. The setup is a bit manual at this point, but possible with the following approach:

  1. Create / update the project specific settings.json file in your .vscode folder (this should already have your python executable once you selected it in the UI)
  2. To get some extra tooling to work, also set PYTHONPATH in a .env file in the project folder

I'm running under Windows 10 with Python 3.8 and my project directory is c:\dev\test1. So far I have just hard coded my PYHTONPATH to include my project src folder and the .venv folder in .env

PYTHONPATH="c:\dev\test1\src;c:\dev\test1\__pypackages__\3.8\.venv\lib;c:\dev\test1\__pypackages__\3.8\.venv\scripts"

In settings.json I have the following:

{
  "python.pythonPath": "c:\\Users\\test1\\AppData\\Roaming\\pyflow\\python-3.8.0\\python.exe",
  "python.linting.flake8Enabled": false,
  "python.linting.enabled": true,
  "python.linting.pylintEnabled": true,
  "python.autoComplete.extraPaths": [
    "${workspaceFolder}",
    "${workspaceFolder}/__pypackages__/3.7/.venv/lib",
    "${workspaceFolder}/__pypackages__/3.8/.venv/lib",
    "${workspaceFolder}/__pypackages__/3.9/.venv/lib"
  ],
  "python.analysis.extraPaths": [
    "${workspaceFolder}/__pypackages__/3.7/.venv/lib",
    "${workspaceFolder}/__pypackages__/3.8/.venv/lib",
    "${workspaceFolder}/__pypackages__/3.9/.venv/lib"
  ],
  "terminal.integrated.env.osx": {
    "PYTHONPATH": "${workspaceFolder}/src:${env:PYTHONPATH}"
  },
  "terminal.integrated.env.linux": {
    "PYTHONPATH": "${workspaceFolder}/src:${env:PYTHONPATH}"
  },

  "terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder}\\src;${env:PYTHONPATH}"
  }
}

python.pythonPath is the Python interpreter path and not the real PYTHONPATH (which comes from my .env file) python.autoComplete.extraPaths and python.analysis.extraPaths were set to allow easier switching between several versions (3.7 - 3.9) and the terminal.integrated.env settings ensure some extra tooling running in the integrated terminal uses the right folder as well. Otherwise, only a debug environment seems to use the extraPath settings.

For using one vscode Repl plugin, which didn't seem to handle the PYHTONPATH properly, I also wrote a dynamic patch to include the .venv folder in sys.path directly:

import sys, os

x_path = sys.path
base = x_path[0] + ''
x_path = sorted(list(set(x_path)))
s_path = str(x_path)
if not ('__pypackages__' in s_path):
    print('patching path')
    sep = os.sep
    z_path = x_path[1:]
    s_base = (
        base
        + sep
        + '__pypackages__'
        + sep
        + str(sys.version_info[0])
        + '.'
        + str(sys.version_info[1])
    )
    lib = s_base + sep + 'lib'
    scripts = s_base + sep + 'Scripts'
    sys.path = [base, lib, scripts, *z_path]
# put the normal code below

This approach is a bit extreme and shouldn't be needed for most use cases.

clawsl avatar Jul 27 '21 06:07 clawsl