3

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.

2
  • Have you tried to configure terminal.integrated.env.windows in the settings.json file? And the tools means linter or formatter make you must edit it in the .env file? Commented Mar 2, 2022 at 7:52
  • 1
    That would work for the integrated terminal, but I'm not using that. I'm more concerned about features like notebooks, mypy, linters etc. Some of them may have individual configs too, but is there a way to set my PYTHONPATH env variable globally to the workspace root, from inside vscode? Commented Mar 2, 2022 at 9:25

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That would work. I still hope there is a way to configure that from inside vscode. Because otherwise I'd need a startup file for every project I work on, because they have different paths.
@Gerenuk, I agree, and would be interested in such a solution as well. But I've been struggling a lot with these type of path settings in VSCode as well, and I think support for these type of things is still limited, unfortunately :-/
0

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}",

2 Comments

I've just tried (again), but it does not find mysrc from a notebook (which is not in the root itself; Python would use the current directory to search by default)
@Gerenuk Could you have a look at the update?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.