Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set PYTHONPATH for all tools in Vscode (on Windows)?

I have a folder mysrc in my project root and I'd like to make all tools of VScode work with import mysrc. The only way I found to make it work is to put the full absolute path PYTHONPATH=<fullpath_to_workfolder> in the .env file. But ideally I'd like to use relative paths to the workfolder.

How can I do that?

All suggestions I found (also here) somehow do not work. ${workspaceFolder} is empty. PYTHONPATH=. does not work. Ideally I'd configure a single PYTHONPATH and not for every tool (terminal, notebooks, mypy, ...). And even my solution for whatever reason duplicates PYTHONPATH=<fullpath_to_workfolder>;<fullpath_to_workfolder> on Windows when I inspect this variable in my code. I believe on Linux I did not have issues.

like image 259
Gerenuk Avatar asked Aug 27 '26 05:08

Gerenuk


1 Answers

I'm not sure if this is what you want, but you could create a batch file with the following lines:

@echo off
set PYTHONPATH=%cd%
"%LOCALAPPDATA%\Programs\Microsoft VS Code\Code.exe"

(assuming default VS Code installation directory — you might have to change the last line if you installed it in another directory.)

This would call VS Code with the PYTHONPATH environment variable set to the directory from where you call it. You can confirm this by opening a terminal window in VS Code and typing echo %PYTHONPATH%. All processes spawned from VS Code will inherit the environment variables, so this should work for all tools.

If you copy the batch script to another project and call it from there, all tools would then use that directory.

like image 129
wovano Avatar answered Aug 28 '26 18:08

wovano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!