How to set environment variables for Python Interactive Window (aka IPython)?
See original GitHub issueHi there,
I am using version 2019.6.24221
with Anaconda 2019.07 and Python 3.7.3 under Windows 8.1.
I am working at a software project where all python files are located in a subfolder “src”. Hence, I added
"terminal.integrated.env.windows": {
"PYTHONPATH" : "${workspaceFolder}/src;${env:PYTHONPATH}"
}
to my local workspace settings.json
.
When choosing “Run Python File inTerminal” via right-click on any of my Python files, it is exectued without any problems, because all the modules in the local directory src
are found.
However, when I choose “Run Current File in Python Interactive Window”, I get a ModuleNotFoundError
in the Python Interactive Window, because my local source code files are not found.
When executing the IPython magic command %env
, I can also see that the environment variable PYTHONPATH
is not set in the Python Interactive Window environment.
How can I tell IPython/Jupyter about mylocal src
subfolder? Can I set project specific environment variables for IPython/Jupyter somewhere?
On Stackoverflow I have only found this post: https://stackoverflow.com/questions/53275420/how-can-i-set-enviroment-variables-for-data-science-mode-of-vscode which did not offer any solution.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:29
- Comments:25 (12 by maintainers)
Top GitHub Comments
This is a problem when loading Jupyter notebooks inside VSCode as well.
In short, the problem is that your environment variables (that you may have defined in .zshrc/.bashrc, etc) are not automatically loaded by the local notebook server.
Two solutions:
.env
file in your project root, followed by their value (no quotes or exports necessary), for instance like this:and reload vscode.
Hi Rich, thanks for the update. If there weren’t other unintended consequences, the fix for the second sounds like it’d be useful. Having said that, it still feels like a common project folder configuration setting specifically for source path that existed ‘above’ both the
.env
andterminal.integrated.env.*
settings, that could then be automatically populated in both internal and external tools, is a potentially simpler way forward given that the same paths are likely required in all runtimes.Given this seems like a fairly common scenario (a python data science project with a set of shared modules in a source tree, and a set of tests / notebooks / exploratory work in a separate folder), do you think it’s worth documenting the best current solution somewhere within the python extension documentation? I just know that it took me quite some time to figure out how to make this all work. It seems right now (unless you’ve got better advice) that absolute paths in the
.env
file is the best way to go. I could imagine to make this work across different machines / platforms, one could have a user-level default.env
on each machine to contain the absolute path for each machine in a platform-appropriate manner.