Skip to content

Support loading environment from .envrc with direnv #9965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ahgraber opened this issue May 10, 2022 · 6 comments
Closed

Support loading environment from .envrc with direnv #9965

ahgraber opened this issue May 10, 2022 · 6 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug triage-needed Issue needs to be triaged

Comments

@ahgraber
Copy link

Environment data

  • VS Code version: 1.67.0
  • Jupyter Extension version (available under the Extensions sidebar): v2022.4.1011282140
  • Python Extension version (available under the Extensions sidebar): v2022.6.0
  • OS (Windows | Mac | Linux distro) and version: Mac 12.3.1 (MacbookPro 14, M1 Pro)
  • Python and/or Anaconda version: conda 4.11.0, mamba 0.16.0, python 3.9./ /3.10.
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): conda
  • Jupyter server running: Local

Expected behaviour

If I start an interactive session from a directory with a .envrc file that is loaded with direnv, the environmental variables that are loaded with direnv should be available via os.environ.

Actual behaviour

None of the variables that are present in my local environment via direnv are available.

Steps to reproduce:

  1. assuming direnv is installed, create .envrc with variables to be loaded and run direnv allow .
# direnv .envrc file
export TEST="this_is_a_test"
  1. in same directory, create a python file that we will run via the interpreter
#%%
import os

#%%
"TEST" in os.environ
  1. Run the cells. The interpreter python environment will not find the env var "TEST", although the variables will be available if we run the same python commands in the terminal python environment

Additional references

There may be some overlap with the vscode-python extension, where they seem to have deemed this feature "not important" even though it is significantly workflow- and expectation- breaking.

Thanks for the suggestion! We talked about it with the team and we have unfortunately decided we will not be moving forward with this idea. We think there isn't an enough widespread need for this to warrant the maintenance cost for the feature.

Originally posted by @luabud in microsoft/vscode-python#14250 (comment)

Also requested as a feature in microsoft/vscode-python#18220 (comment), and .envrc/direnv proposed as a solution in microsoft/vscode-python#18250 (comment)

@ahgraber ahgraber added the bug Issue identified by VS Code Team member as probable bug label May 10, 2022
@github-actions github-actions bot added the triage-needed Issue needs to be triaged label May 10, 2022
@rchiodo
Copy link
Contributor

rchiodo commented May 10, 2022

Thanks for the suggestion.

My guess is that since nobody else has asked for this in notebooks yet, the odds are we won't be pursuing this.

However everything here is open source and we gladly except PRs. The tricky part here is that we don't start our kernels using a terminal. So any terminal additions would be difficult to use.

.env file support is supported though. Could you use that instead?

@ahgraber
Copy link
Author

Yeah, .env is certainly an option; however most of our workflows currently use .envrc, so I'll have to figure out a way to keep the two in sync.

Thanks for the quick response.

@NktBkzn
Copy link

NktBkzn commented Dec 19, 2022

@rchiodo
I would also love to have this feature. So I am the second one asking:)
I work with a repo which keeps the definition of env variables in .envrc file. I also run some experiments in jupyter lab.
It would be great to be able to use env variables defined in .envrc file inside notebooks

@ahgraber
Copy link
Author

@NktBkzn - I've resolved by moving variables needed in the python environment to a .env file and then loading that .env file with direnv by adding the following line to the bottom of my .envrc.

dotenv_if_exists .env

It's hacky, but it works... and I think we're a small enough population that microsoft probably won't add this feature.

@introom
Copy link

introom commented Dec 30, 2022

@NktBkzn - I've resolved by moving variables needed in the python environment to a .env file and then loading that .env file with direnv by adding the following line to the bottom of my .envrc.

dotenv_if_exists .env

It's hacky, but it works... and I think we're a small enough population that microsoft probably won't add this feature.

@ahgraber Please use this extension: https://github.com/direnv/direnv-vscode

@emddarn
Copy link

emddarn commented Sep 5, 2023

Jupyter in vscode doesn't run bashrc or zshrc when it starts. Adding
"jupyter.runStartupCommands": [direnv allow .]
in settings seems to be working for me

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug triage-needed Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

5 participants