skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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

  2. /etc/environment is the second boot file when logging into the Linux system.

    You can use the following command to open, or open the file in the directory:

    ~$ sudo gedit /etc/environment
    

    Environment variables are defined here. We can add environment variables you need, such as:

    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
    
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/yan/anaconda2/bin"
    

    So we add anaconda2 to PATH environment variable.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search