top of page
Writer's pictureThe Tech Platform

How to Read Strings From a .env File With Python, Express.JS, and Go



A .env, or environment file, is one of the essential files in a structured, secure programming project. You might've come across recommendations to store your app's configuration in environment variables. An .env file can help smooth the process.


But how does a .env file work? And how can you create one and read from it in Python, Express.js, or Go? You'll find out in this article.


What Is a .env File?

The .env file holds your app's configuration details in environment variables. The file helps you abstract this information to avoid exposing it in your code.


There's no limit to what can be in the .env file. Deciding what you include depends on the configuration that's relevant to your app. Details could include API keys, URLs, database URIs, and access tokens.


How to Read Data From the .env File

Your programming language won't read the contents of a .env file automatically. You'll need to use a library to read and parse the file. Here's how to do that in Python, Node.js (Express), and Go.


How to Read the .env File in Python

You need a few libraries to process the .env variables in Python. These include the os library and the dotenv third-party package.


The load_dotenv method from dotenv provides the functionality to read data from a .env file.


To begin, create a Python virtual environment and install the dotenv package using pip:

pip install dotenv

You do not need to install the os package as it's built into Python.


Next, create a file named .env in your project root and add some configuration values. For example:

App_password = idStringSecret
Python_version = Python 3.9.7

Now, in your Python file:

from dotenv import load_dotenv
import os
 
# Use load_env to trace the path of .env:
load_dotenv('.env') 
 
# Get the values of the variables from .env using the os library:
password = os.environ.get("App_password")
languageVersion = os.environ.get("Python_version")
 
print(password)
# idStringSecret
print(languageVersion)
# Python 3.9.7


Reading the .env File in Node.js (Express)

The process for loading environment variables in Express.js is slightly different from Python's.

First, you'll need to install the dotenv library using npm. This section assumes that you've started a node.js project and already running a server.


In your project root's terminal:

npm install dotenv --save

Now, create a .env file in your project root. Assume the following variables are in the file:

App_password = idStringSecret
Node_version = v14.17.5

To read variables from .env, type the following code in your JavaScript:

const dotenv = require('dotenv')
 
dotenv.config()
 
password = process.env.App_password
nodeVersion = process.env.Node_version
 
console.log(password)
// idStringSecret
 
console.log(nodeVersion)
// v14.17.5


How to Read the .env File in Go

Go also requires installation of a package to read variables from the environment file: godotenv.

You use this library to point Go to the .env file path. Then you'll subsequently use the Go's built-in os class to read the data.


Open the command line to your project root folder and run the following command to install Go's godotenv library:

go get -d github.com/joho/godotenv/cmd/godotenv

Assume that the .env file in your Go project has the following variables:

App_password = idStringSecret
Go_version = go1.17.5

Here's how to read the variables in that environment file:

package main
import (
 "fmt"
 "os"
 "github.com/joho/godotenv"
)
 
func main() {
 // Load the .env file:
 godotenv.Load(".env")
 
 // The the variables using os package:
 password := os.Getenv("App_password")
 goVersion := os.Getenv("Go_version")
 
 fmt.Println(password)
 // idStringSecret
 
 fmt.Println(goVersion)
 // go1.17.5
}

That's it. You can now use the .env file to hold environment configurations for apps you build with Go, Python, or Express.



Source: Makeuseof.com


The Tech Platform

0 comments

Recent Posts

See All

Commenti


bottom of page