PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 19, 2022

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

 August 19, 2022     environment-variables, python, ubuntu, visual-studio-code     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing