microsoft / vscode-python-debugger

Python debugger (debugpy) extension for VS Code.
https://marketplace.visualstudio.com/items?itemName=ms-python.debugpy
MIT License
40 stars 17 forks source link

add ${workspaceFolder} to "PYTHONPATH" in launch.json by default #343

Open Akopov4 opened 1 month ago

Akopov4 commented 1 month ago

Hi, I'm new User of VScode. Previously I used Jetbrains Pycharm and not for a long time ago switched to VSCode. Please add "env": { "PYTHONPATH": "${workspaceFolder};${env:PYTHONPATH}" } as default configuration parameters, when create to launch.json Pycharm did it for a long time ago Thank you in advance

rchiodo commented 1 month ago

Thanks for the issue. Pylance doesn't actually handle debugging of Python code. Transferring.

karthiknadig commented 1 month ago

@Akopov4 You see an issue when you run or you see it with handling of imports in the editor?

Akopov4 commented 1 month ago

it's not an issue, it's feature request

Akopov4 commented 1 month ago

@karthiknadig

heejaechang commented 1 month ago
"env": {
"PYTHONPATH": "${workspaceFolder}"
}

without it, when a user tries to run python file in debugger, import statement rooted from workspace root won't be resolved at runtime even if it is resolved at editing time due to our import resolve heuristic.

root/
        lib/
             __init__.py
             module.py
             currentfile.py

currentfile.py
import module # this will work at runtime and edit time
import lib.module # this won't work at runtime but will work at edit time.

user might want it to just work when debugging?

that said, if user run currentfile.py from terminal, it still won't work unless user add root to sys.path in some ways such as setting PYTHONPATH env var or sys.path.append(...) from code.

user could use .env for it to be set on terminal but when user run it in regular command line it won't work again.

but it will at least make it to just work for debugger?

BreathBlush commented 1 month ago

maybe "PYTHONPATH": "${workspaceFolder};${env:PYTHONPATH}", would be better for reason that there were already PYTHONPATH environmental variable set

Akopov4 commented 1 month ago

@heejaechang usually directories like .vscode, .idea and so on are included in gitignore file. So they don't affect production codes. This configuration is used by debugger and by "Run python file" in the editor. Soultion of @V4n1sh is better then my one

github-actions[bot] commented 1 month ago

Thanks for the feature request! We are going to give the community 60 days from when this issue was created to provide 7 👍 upvotes on the opening comment to gauge general interest in this idea. If there's enough upvotes then we will consider this feature request in our future planning. If there's unfortunately not enough upvotes then we will close this issue.

shtratos commented 1 week ago

I support this feature request, in general I think we can do better with out-of-the-box experience - I was surprised how much effort and setup it takes to just start debugging any Python code in VSCode for a new project as opposed to PyCharm where it "just works".