Friday, August 19, 2022

[FIXED] How to add Environment Variables in Visual Studio Code on Ubuntu

Issue

I am working on a python project using Visual Studio Code on Ubuntu OS and I need to set environment variables without using the integrated terminal, I want to be able to set it using a file or an efficient medium asides my terminal.

Please help


Solution

So I was able to find a solution that enabled me specify my variables in a .env file in Visual Studio Code

I used a python module called python-dotenv the setup for installing it can be found in https://pypi.org/project/python-dotenv/

So follow these steps:

  1. In your Visual Studio Code integrated terminal, install python-dotenv using pip install python-dotenv

  2. In your python code add this at the top of the code

from dotenv import load_dotenv

load_dotenv() 
  1. In your code folder i.e where the python code specified above lies, create a file called .env

  2. Now specify all your environment variables inside the .env file e.g

DB_PORT=1212
DB_NAME=test_db
DB_USER=test_user
  1. Now run the python code and watch the environment variable work in action


Answered By - Oyindamola Victor
Answer Checked By - Katrina (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.