[英]use .env file in vscode when connected to Ubuntu server via ssh
I have created a workspace on my local machine to connect to Linux server.我在本地机器上创建了一个工作区来连接到 Linux 服务器。 I connect to this server in VS Code using SSH extension as outlined in these docs.我使用 SSH 扩展在 VS Code 中连接到此服务器,如这些文档中所述。 https://code.visualstudio.com/docs/remote/ssh#_managing-extensions
. https://code.visualstudio.com/docs/remote/ssh#_managing-extensions
。
When I run the .py
application in my Local Workspace I get the error UndefinedValueError: CREDENTIALS not found. Declare it as envvar or define a default value.
当我在本地工作区中运行.py
应用程序时,出现错误UndefinedValueError: CREDENTIALS not found. Declare it as envvar or define a default value.
UndefinedValueError: CREDENTIALS not found. Declare it as envvar or define a default value.
The .py
application is in the same directory as the .env
file which houses the CREDENTIALS
variable on the Linux server and when run on the Linux server works fine.. .py
应用程序与.env
文件位于 Linux 服务器上的CREDENTIALS
变量所在的同一目录中, .env
在 Linux 服务器上运行时可以正常工作。
How do I tell vscode on my local machine to access the .env file when Im running the .py
application locally?当我在本地运行.py
应用程序时,如何告诉我本地机器上的 vscode 访问 .env 文件?
from decouple import config
credentials = config('CREDENTIALS')
Only started working in this type of coding environment today so apologies in advance if I have missed something very simple...今天才开始在这种类型的编码环境中工作,所以如果我错过了一些非常简单的东西,请提前道歉......
Use the python-dotenv library, nice and easy.使用 python-dotenv 库,简单易用。 Install via通过安装pip install python-dotenv
Then in your file call然后在你的文件调用中
from dotenv import load_dotenv
Then just call load_dotenv()
from dotenv import load_dotenv
然后调用load_dotenv()
And all your env variables declared in the file will be put into the environment table.并且您在文件中声明的所有 env 变量都将放入环境表中。 As long as that config
function is grabbing env variables it should work.只要该config
函数正在获取环境变量,它就应该可以工作。 If it doesn't, you could use如果没有,你可以使用
Import os
from dotenv import load_dotenv
load_dotenv()
EnvVar = os.getenv('CONFIG')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.