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

发布于 2025-01-10 19:38:23 字数 602 浏览 0 评论 0原文

我的项目根目录中有一个文件夹 mysrc ,我想让 VScode 的所有工具都可以与 import mysrc 一起使用。我发现使其工作的唯一方法是将完整的绝对路径 PYTHONPATH= 放入 .env 文件中。但理想情况下我想使用工作文件夹的相对路径。

我怎样才能做到这一点?

我发现的所有建议(也在此处)都不起作用。 ${workspaceFolder} 为空。 PYTHONPATH=. 不起作用。理想情况下,我会配置一个PYTHONPATH,而不是为每个工具(终端、笔记本、mypy...)配置。当我在代码中检查此变量时,即使我的解决方案出于某种原因也会在 Windows 上重复 PYTHONPATH=; 。我相信在 Linux 上我没有遇到问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

︶ ̄淡然 2025-01-17 19:38:23

我不确定这是否是您想要的,但您可以创建一个包含以下几行的批处理文件:(

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

假设默认 VS Code 安装目录 - 如果您将其安装在另一个目录中,则可能需要更改最后一行.

这将调用 VS Code,并将 PYTHONPATH 环境变量设置为调用它的目录。
您可以通过在 VS Code 中打开终端窗口并输入 echo %PYTHONPATH% 来确认这一点。从 VS Code 生成的所有进程都将继承环境变量,因此这应该适用于所有工具。

如果您将批处理脚本复制到另一个项目并从那里调用它,则所有工具都将使用该目录。

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.

帅哥哥的热头脑 2025-01-17 19:38:23

抱歉,但是当我将这些添加到 .env 文件中时它就可以工作:

PYTHONPATH=.\

或者

PYTHONPATH=.

您可以重新确认吗?

更新:

.env 文件对 Jupyter NoteBook 没有影响,但它适用于 linter、格式化程序、IntelliSense 和测试工具。

您可以配置它来实现它:

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

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}",
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文