简体   繁体   中英

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.

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.

Sorry, but it works when I add these in the .env file:

PYTHONPATH=.\

or

PYTHONPATH=.

Could you reconfirm it?

Update:

.env file has no influence on the Jupyter NoteBook, but it works on linters, formatters, IntelliSense, and testing tools.

You can configure this to achieve it:

"jupyter.notebookFileRoot": "${workspaceFolder}",

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM