在VS代码中运行Python3脚本,除非运行调试器(MACOS),否则不识别环境变量
要澄清,我正确设置了环境变量的.ENV文件,以及当我通过内置终端运行代码(默认播放按钮)或通过代码Runner插件输出时,尝试抓住这些环境变量的尝试总是最终出现无效的。但是,同一件事在通过调试器运行时毫无问题。
从我的研究中,这是一个相对广泛的问题。我遵循官方 documentation 我知道我正确地格式化了.env文件,因为它可以正常使用。与调试器。
我检查了我在Stackoverflow上发现的所有帖子,并尝试了数十种解决方案的VS-Code Github。这些提到的解决方案中有很多对我不起作用,或者蒙特雷过时。我已经尝试了几件事,包括:
- 禁用代码Runner
- 将文件重命名为
dev.env
等,同时反映了VS代码设置中的Env File Path - 将.ENV文件放在VSCODE和。 VENV文件夹在反射路径时,
- 我在我在网上找到的设置JSON中尝试了一系列命令。抱歉,我不会更具体,我尝试了很多。
- 我还没有尝试过将环境变量安装到Python路径上,如果这样做有意义,我还不清楚该怎么做。
我正在使用运行最新的MACOS MONTEREY 12.3.1,最新VS代码1.66.2和最新版本Python 3.10.4的Intel Mac的最后一代。当前,我有默认的env文件路径集:$ {workspacefolder}/。env
和一个名为.env
的文件。
除非我缺少某些东西,否则我不确定这将在这么长时间内无法解决和/或缺少解决方案。无论我尝试什么,我都想知道几乎每个遇到这个问题的人都是最好的/首选解决方案。感谢您的帮助。
To clarify I set up the .env file for environment variables correctly and when I run the code either via the built-in terminal (default play button) or to output via the Code Runner plugin, the attempts to grab those environment variables always end up null. However, the same thing works when running via debugger with no problem.
This has been a relatively widely experienced issue ~3 years ago from my research. I followed the official documentation exactly and I know I formatted the .env file right because it works with the debugger.
I checked all posts I found across StackOverflow and the VS-Code GitHub trying dozens of solutions. A lot of these mentioned solutions didn't work for me or are out of date for Monterey. I've tried several things from these and on my own including:
- Disable Code Runner
- Rename file to
dev.env
and others, while reflecting Env File path in VS Code settings - Placing .env file in vscode and .venv folders while reflecting in path
- I've tried a series of commands in the settings JSON I've found online. Sorry, I'm not going to be more specific, I've tried so many.
- I haven't tried installing environment variables to the python path yet, something I'm unclear on how to do if it makes sense to do so.
I'm using the last-gen of Intel Mac running the latest macOS Monterey 12.3.1, the latest VS Code 1.66.2, and the latest version of Python 3.10.4. Currently, I have the default Env File path set: ${workspaceFolder}/.env
and a file named .env
in the workspace folder.
Unless I'm missing something I'm not sure how this would have gone unresolved for this long and/or I'm missing the solution. Regardless of what I've tried, I'd like to know what the best/go-to solution is for almost everyone faced with this problem. Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论