Using .env files to set environment variables in Windows
Asked Answered
L

5

11

I am working on a set of projects that use .env files for configuration, where each .env file exports a number of environment variables. The files are setup like;

export VARIABLE1=foo

I use the Windows Linux subsystem a lot for this, but would like to also be able to run these projects on my windows machine. The projects in question are Golang.

IS there any easy way to programatically set Windows environment variables (preferably termporarily) from these .env files? These is mostly so I can debug within VSCode.

Lewellen answered 4/2, 2018 at 10:53 Comment(4)
Please clarify how the .env is loaded exactly. Go doesn't handle .env natively, you're probably using godotenv. It sets the env variables for the current process (if using it as a library) and might work on windows. If you're sourcing the .env before running the go program, then that's even more detached and has nothing to do with Go.Reinold
Sorry forgot that part. Typically in a linux/bash environment I just source foo.env, which loads the environment variables within the file. This isn't necessarily specific to Go, it's just that I am having to use this method with a Go app.Lewellen
Ok, then Godotenv mentioned above week so all that for you, both in Linux and windows.Reinold
direnv does this, although it uses .envrc by default. Not sure if this can be changed.Descombes
M
7

If you are on Windows, you can use powershell to read your .env file and load the key=value pairs into session-level environment variables that your app can read. Toss the code below into a file called DotEnvToEnvVars.ps1 (or whatever you want to call it). Then run the script passing the path to your .env file: C:> ./DotEnvToEnVars.ps1 ./myproject/.env -Verbose

Include optional params:

  • -Verbose to see what environment variables were set

  • -Remove to delete all the environment variables with names found in that .env file

  • -RemoveQuotes to strip the " and ' from surrounding the value side of the key/value pairs in your .env before making environment variables out of them

param(
    [string]$Path,
    [switch]$Verbose,
    [switch]$Remove,
    [switch]$RemoveQuotes
)

$variables = Select-String -Path $Path -Pattern '^\s*[^\s=#]+=[^\s]+$' -Raw

foreach($var in $variables) {
    $keyVal = $var -split '=', 2
    $key = $keyVal[0].Trim()
    $val = $RemoveQuotes ? $keyVal[1].Trim("'").Trim('"') : $keyVal[1]
    [Environment]::SetEnvironmentVariable($key, $Remove ? '' : $val)
    if ($Verbose) {
        "$key=$([Environment]::GetEnvironmentVariable($key))"
    }
}
Maynard answered 25/4, 2022 at 14:50 Comment(0)
D
1

i used godotenv library.

Add configuration values to a .env file at the root of project:

PORT=8080
NAME=FlowAPI

main.go

package main

import (
    "log"
    "github.com/joho/godotenv"
    "fmt"
    "os"
)

func init() {
    // loads values from .env into the system
    if err := godotenv.Load(); err != nil {
        log.Print("No .env file found")
    }
}

func main() {
    // Get the PORT environment variable
    port, exists := os.LookupEnv("PORT")
    if exists {
        fmt.Println(port)
    }
}
Disrespectful answered 8/3, 2019 at 2:17 Comment(0)
D
1

Create a text file called ".env" (you could actually call it anything I guess, but this makes it brief and descriptive). Put this file in any folder of your choice... well away from any Python code you might make public, e.g. GitHub, GitLab, etc. For example, mine is located at "E:/Python/EnvironmentVariables/.env".

Edit the file in your favourite text editor, and create your secrets. You can have all of your secrets for all of your projects in this one file if you wish, [Headings] are optional and for your benefit only. Make sure all the key names are different - add the project name to the beginning or end if you wish:

[MyProject]
MyUsername="Fred Bloggs"
MyEmail="[email protected]"
MyPassword="fvghtyu7i890ol"

[MyOtherProject]
MyAPIKey_MyOtherProject="FMu4ejyK-qQLBasTs7iHx*SFN5fF"
email_port=110
DEBUG=True

Now in your python program do this to get access to any secret you want, in any project!

import os
from dotenv import load_dotenv

load_dotenv("E:/Python/EnvironmentVariables/.env")
USER_NAME = os.getenv("MyUsername")
API_KEY = os.getenv("MyAPIKey_MyOtherProject")

print(USER_NAME, API_KEY)
Denote answered 17/1, 2021 at 2:5 Comment(1)
Question was for golang specific answer.Willner
P
1

Old question that got bumped today with a new answer, but there's a supported feature for doing this (wslenv) that hasn't been mentioned.

The OP mentions that they have Windows Subsystem for Linux, but they want to have the .env files that they use in WSL available in the Windows environment.

Assuming the variable names are static, this is fairly straightforward (no script/parsing necessary):

Take, for example, an .env with:

export VARIABLE1=foo
export VARIABLE2=bar
export VARIABLE3=baz

Start in WSL/Bash:

cd /mnt/c/
source /path/to/.env
# Substitute powershell.exe for older version
WSLENV=$WSLENV:VARIABLE1:VARIABLE2:VARIABLE3 pwsh.exe

You will then be in a PowerShell instance where the environment is set with the desired variables from your WSL .env file.

Executing any Windows process from there, such as code, will propagate the environment to that subprocess.

So, run code, then open a new terminal inside VSCode.

echo $env:VARIABLE1

... will correctly respond with foo.

Alternative if the variable names are dynamic

Or convenience method for collecting the variable names

If you want to just generate a list of the variables from your .env file, something like the following will work:

In WSL/Bash:

source ./.env
for v in $(sed -E 's/export *([^=]*).*/\1/' ./.env); do export WSLENV=$WSLENV:$v; done
# Substitute powershell.exe for older version
pwsh.exe

That will process the .env file to look for the variable names that need to be added to the $WSLENV variable.

Profound answered 25/4, 2022 at 20:12 Comment(0)
F
0

Idk why, but first answer doesn't work for me in win11. I did this: Create file with *.psl extension with the following contents. Then run them in the IDE terminal.

[string[]]$variables = Get-Content -Path .env -Encoding utf8

foreach($var in $variables) {
    $keyVal = $var -split '=', 2
    $key = $keyVal[0].Trim("'")
    $val = $keyVal[1].Trim("'")
    "$key=$val"
    [Environment]::SetEnvironmentVariable($key, $val)
}
Flow answered 14/3 at 15:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.