简体   繁体   English

如何为 Vscode(在 Windows 上)中的所有工具设置 PYTHONPATH?

[英]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 .我的项目根目录中有一个文件夹mysrc ,我想让 VScode 的所有工具都与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.我发现让它工作的唯一方法是将完整的绝对路径PYTHONPATH=<fullpath_to_workfolder>放在.env文件中。 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. ${workspaceFolder}是空的。 PYTHONPATH=. does not work.不起作用。 Ideally I'd configure a single PYTHONPATH and not for every tool (terminal, notebooks, mypy, ...).理想情况下,我会配置一个PYTHONPATH而不是为每个工具(终端、笔记本、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.当我在代码中检查此变量时,甚至出于某种原因,我的解决方案也会在 Windows 上复制PYTHONPATH=<fullpath_to_workfolder>;<fullpath_to_workfolder> I believe on Linux I did not have issues.我相信在 Linux 上我没有遇到任何问题。

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. ) 假设默认的 VS Code 安装目录——如果你将它安装在另一个目录中,你可能需要更改最后一行。

This would call VS Code with the PYTHONPATH environment variable set to the directory from where you call it.这将调用 VS Code,并将 PYTHONPATH 环境变量设置为您调用它的目录。 You can confirm this by opening a terminal window in VS Code and typing echo %PYTHONPATH% .您可以通过在 VS Code 中打开终端 window 并键入echo %PYTHONPATH%来确认这一点。 All processes spawned from VS Code will inherit the environment variables, so this should work for all tools.从 VS Code 生成的所有进程都将继承环境变量,因此这应该适用于所有工具。

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:抱歉,但是当我在.env文件中添加这些时它起作用了:

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. .env文件对 Jupyter NoteBook 没有影响,但它适用于 linters、格式化程序、IntelliSense 和测试工具。

You can configure this to achieve it:您可以配置它来实现它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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