简体   繁体   English

通过 ssh 连接到 Ubuntu 服务器时,在 vscode 中使用 .env 文件

[英]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.

相关问题 通过 ssh 和 python 将文件复制到远程服务器 - Copying file to remote server via ssh with python Vscode - .env 文件中的变量扩展 - Vscode - variable expansion in .env file Ubuntu Server和SSH上的matplotlib错误 - Error with matplotlib on Ubuntu Server and SSH 为什么 VSCode 无法使用远程 ssh 服务器打开同一目录中的文件? - Why cant VSCode using remote ssh server open file in the same directory? 无法连接ubuntu上的套接字服务器 - The socket server on ubuntu can't be connected VSCode Python:在 vscode 的调试或测试模块期间,Pydantic “env_file”被 .env 文件覆盖 - VSCode Python: Pydantic "env_file" overridden by .env file during vscode's Debugging or Testing module ubuntu /usr/bin/env: python: 没有那个文件或目录 - ubuntu /usr/bin/env: python: No such file or directory 连接到 linux 服务器时无法在 Visual Studio 中运行活动文件 - Cannot run active file in visual studio when connected to linux server 如何将 DJANGO 应用程序连接到通过 SSH 连接到堡垒的 AWS RDS - how to connect DJANGO app to AWS RDS connected to a bastion via SSH 代码。 Pylance 无法通过 SSH 连接工作 - VSCode. Pylance doesn't work via SSH connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM