question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use environment variables defined in `.env` file when running code in a terminal

See original GitHub issue

@brettcannon I’ve been using PYTHONPATH variable in .env for debugging using experimental debugger (without having to pip install PTVSD). However running in terminal doesn’t inherit these variables, I’ve had to export them manually.

What do you think about this feature?

Technical solution outlined in #4310 Need spec to provide UX (prompting users to load env, etc)

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:116
  • Comments:52 (7 by maintainers)

github_iconTop GitHub Comments

76reactions
joepariscommented, Aug 2, 2020

I would like to ask for some clarification on this issue, please. As of 2he VS Code documentation on Python Environments as of 8/2/20 says:

An environment variable definitions file is a simple text file containing key-value pairs in the form of environment_variable=value, with # used for comments…

By default, the Python extension looks for and loads a file named .env in the current workspace folder, then applies those definitions…

I interpret this to mean that when VS Code finds a .env in the root of the workspace it will read the file and set any environment variables found in it in the current shell before the code is executed.

I have a .env file in the root of my workspace as follows:

DB_USER=my_db_user
DB_PASS=my_db_password_123!

I also have a file, envtest.py, in the root of the workspace that looks like this:

import os

db_user = os.environ.get("DB_USER")
db_password = os.environ.get("DB_PASS")

print(db_user)
print(db_password)

When I run this code in the integrated terminal (either Powershell or WSL) or if I remote into WSL I get output like the following:

(venv-win) PS C:\Users\joe\Dev\temp3> & c:/Users/joe/Dev/temp3/venv-win/Scripts/python.exe c:/Users/joe/Dev/temp3/envtest.py
DB_USER = None
DB_PASS = None

😖

When I run this code in the debugger I get the expected output; i.e. the environment variables are set and their values are printed to the screen.

(venv-win) PS C:\Users\joe\Dev\temp3>  cd 'c:\Users\joe\Dev\temp3'; & 'c:\Users\joe\Dev\temp3\venv-win\Scripts\python.exe' 'c:\Users\joe\.vscode\extensions\ms-python.python-2020.7.96456\pythonFiles\lib\python\debugpy\launcher' '51594' '--' 'c:\Users\joe\Dev\temp3\envtest.py'
DB_USER = my_db_user
DB_PASS = my_db_password_123!

😕

This behavior is maddeningly inconsistent and seemingly contradicts the documentation. 😠 Is there any plan to standardize the behavior and/or correct the documentation?

68reactions
dakotahawkinscommented, Mar 5, 2018

The whole "python.envFile" feature needs much better documentation/support (#544)…

I don’t know if or how it works, I don’t know if I can or how to refer to other variables inside .env (e.g. Does PYTHONPATH=${PYTHONPATH}:${workspaceFolder}/utility work?), etc.

From an hour of trying to use it, it appears nobody on the internet knows what’s going on. It’s either people who can’t get it to work either or people saying it does work but not giving realistic examples… grrr!!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Use Environment Files with env-cmd | DigitalOcean
Learn how to add handle environment variables with the env-cmd package.
Read more >
Working with Environment Variables in Node.js - Twilio
An alternative way to load .​​ This command-line tool will load a . env file, initialize the values using dotenv, and then execute...
Read more >
How to use a .env file to load environment variables in a dev ...
Loading environment variables using a . env file when working with Remote - Containers or GitHub Codespaces. Check out the docs for more...
Read more >
Use environment variables in Terminal on Mac - Apple Support
You can create environment variables and use them to control the behavior of a command without modifying the command itself. For example, you...
Read more >
What is .env ? How to Set up and run a .env file in Node?
Environment variables are already included in the Node.js ecosystem, which gives them a significant benefit over alternative configuration ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found