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.

How to set environment variables for Python Interactive Window (aka IPython)?

See original GitHub issue

Hi 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 srcsubfolder? 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:closed
  • Created 4 years ago
  • Reactions:29
  • Comments:25 (12 by maintainers)

github_iconTop GitHub Comments

11reactions
m-yaghinicommented, Jan 27, 2021

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:

  1. Write down the environment variables in an .env file in your project root, followed by their value (no quotes or exports necessary), for instance like this:
DYLD_LIBRARY_PATH=/Users/user/SCIPOptSuite-7.0.2-Darwin/lib

and reload vscode.

  1. Load your own server, outside of vscode (in a terminal for instance) and change the Jupyter server to “Existing” and enter the server’s URI.
6reactions
crnolancommented, Aug 21, 2020

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 and terminal.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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set environment variables for Python Interactive ...
Hi 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...
Read more >
How do I apply environment variables to Python Interactive ...
The above solution works, but if you change the environment variables in your .env file, you still have to reload the window.
Read more >
Working with Jupyter code cells in the Python Interactive window
Work with Jupyter-like code cells; Run code in the Python Interactive Window; View, inspect, and filter variables using the Variables Explorer and Data ......
Read more >
IPython as a system shell
With no arguments, this displays all environment variables and values. To get the value of a specific variable, use %env var . To...
Read more >
Setting up VS Code for Python Development like RStudio
Simply write your code in a .py file and press CMD+ENTER to execute line-by-line in the Python Interactive Window.
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