James Cook
James Cook

Reputation: 344

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. I connect to this server in VS Code using SSH extension as outlined in these docs. 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.

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..

How do I tell vscode on my local machine to access the .env file when Im running the .py application locally?

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...

Upvotes: 0

Views: 595

Answers (1)

Josh
Josh

Reputation: 328

Use the python-dotenv library, nice and easy. Install via
pip install python-dotenv

Then in your file call

from dotenv import load_dotenv Then just call load_dotenv()
And all your env variables declared in the file will be put into the environment table. As long as that config function is grabbing env variables it should work. If it doesn't, you could use

Import os
from dotenv import load_dotenv
load_dotenv()
EnvVar = os.getenv('CONFIG')

Upvotes: 1

Related Questions