Using system environment variables in spyder
Asked Answered
H

4

14

I have a program coded in Python 2 that I need to run and I would like to run it in spyder via anaconda software. The issue is that to run the program via the terminal I had to previously add in my system environment variables two new variables:

  1. one folder in the computer where some required packages are
  2. a variable with the ip address for the required license.

Although I have these variables define and the program runs without issues in the terminal. When I run it in spyder there is an error because it is not using those variables.

How can I fix this? I head something about anaconda creates virtual environments that is not loading those variables but I could not understand anything else. For example if I use pycharm, outside anaconda, it also works flawlessly.

Heehaw answered 13/2, 2020 at 14:23 Comment(4)
If you want to run the script in spyder, the easiest way would be to just declare the file path as an actual variable and not an environment variablePoint
So spyder does not load the system variables, they must be imported in the beginning of the script is that right?Heehaw
I don't believe that spyder cares about environment variables that are defined outside. The way it constructs its own environment is quite complicated because of IPython and a global namespace between the console and your script. Having re-read your question a few more times, I think I get what you're asking. My guess is that this is folly, or at least bug-prone, but it's just that; a guess. I'll have to leave it to others to answerPoint
My suggestion would be to remove the second question at the end, btw. Your post should ideally focus on one issuePoint
F
9

I believe Anaconda offers instructions on setting environment variables but that approach seemed complicated.

Setting an environment variable no longer seems to be an option in Spyder.

As a workaround Spyder does allow you to specify a startup file: Preferences -> IPython Console -> Startup

In the startup file you just run something like:

import os
os.environ['my_var'] = 'my_value'
os.environ['test'] = 'test_value'

Every time you start a new ipython console this code will run.

Figurant answered 12/1, 2021 at 14:23 Comment(0)
C
1

There's an option in the Tools menu that let's you check/insert/update/remove the currently active environment variables in Spyder (pic for reference).

Spyder Environment Variables Feature

Edit: Regarding your PS question, when you install packages (through pip?) you are doing so through the currently active Python installation, which you can check by running

python -V

Therefore, if you intend to install a package in multiple installations, you need to use the corresponding package manager executable.

Celestinecelestite answered 13/2, 2020 at 15:0 Comment(8)
I already added the required variables in there but, unfortunately, the problem persisted.Heehaw
Let me have a further look thenFreeliving
Btw, are you using IPython console, or running the script?Freeliving
Are your environment variables set on the system or user level? Because I just did some tests, and Spyder only recognizes user-level environment variables.Freeliving
I have tried it now defining them as user variables but still does not import. I do run but it always opens the IPython console. I think I will just use VSCode or pycharm. I was just interested in spyder because its GUI is very similar to MATLAB with which I feel much more confortable.Heehaw
I used Matlab during university, and I tried using Spyder, but I just can't adapt. Personally, I love the customizability of VS Code, and I'm pushing the rest of my Data Science team to migrate to VS Code. Hope you enjoy it.Freeliving
Interesting. The option shown in the screenshot doesn't exist on my Spyder drop-down menu but it's what I was looking for. Instead I have to set the environment on the command line before opening Spyder from that same command line. Even this won't work if I use the Spyder setting "execute in external system terminal" but oh well.Unchristian
It worked for me but I had to restart Spyder after I saved the new environmental variables.Algophobia
D
0

I'm not native english speaker. I try to explain with my best. But if it's not clear, forgive me.

If you want your conda env vars available to your GUI app like Spyder, launch your GUI app from terminal after activating the conda env.

GUI apps launched by desktop don't know them (even if you installed the GUI app in your conda environment).

It's better to think about login, interactive and non-interactive shells and about .bashrc and .bash_profile files. Please read the following text for more details.

Normally, in Unix systems, we have login shells and interactive shells (see this):

A login shell, like the one you get when you first log in, reads specific startup files that configure your environment variables and personalize your experience. Interactive shells launched afterwards, say by opening a new terminal window, don't automatically read those same startup files. So, a login shell sets up your environment, while interactive shells you launch yourself inherit that environment.

You can define your permanent environment variables in .bashrc and .bash_profile. These methods have some differences. Spyder and programs launched by desktop will see the environment variables defined for login shells (defined in .bash_profile). If you launch Spyder from terminal, you will see variables for both login and interative shells (defined in .bashrc or .bash_profile, and variables defined by conda env config vars set).

From RedSwitches:

The main difference lies in when each file is executed. The .bashrc file is executed for non-interactive, command-line shell sessions, while the .bash_profile file is executed for login shell sessions, such as when you log into your system or remotely via SSH.

To see differences, you can define two variables, BASHRC_VAR in your .bashrc and BASHPROFILE_VAR in .bash_profile:

echo "export BASHRC_VAR=\"I'm Defined\"" >> ~/.bashrc
echo "export BASHPROFILE_VAR=\"I'm Defined\"" >> ~/.bash_profile

To apply environment variables to login shells, logout from your desktop and login again. Then run the following program in normal terminal and in the Spyder:

import os

for v in ['BASHRC_VAR', 'BASHPROFILE_VAR']:
    print(v, end=': ')
    try:
        print(os.environ[v])
    except:
        print('Undefined!')

Result from normal terminal:

BASHRC_VAR: I'm Defined
BASHPROFILE_VAR: I'm Defined

Result from Spyder:

BASHRC_VAR: Undefined!
BASHPROFILE_VAR: I'm Defined

If you launch Spyder from terminal, the result will be:

BASHRC_VAR: I'm Defined
BASHPROFILE_VAR: I'm Defined

So, environment variables defined in .bashrc and also variables defined by conda env config vars set are not visible in Spyder. You must define your variables in .bash_profile or you must launch Spyder from the terminal.

Solutions to your problem:

  • Define your environment variables in .bash_profile, and login again.
  • Launch Spyder from terminal (not by clicking on dekstop icons)
  • Use config files (.ini, .config, .yaml, .json) and use configparser or similar solutions.
  • Use environment variables and config files together with some defined priorities
Daubigny answered 15/5 at 14:34 Comment(2)
"launch your GUI app from terminal after activating the conda env." I think that's what I had tried and it didn't work. I am in Windows though, if that matters.Inkle
@Inkle I think it matters. I didn't have Windows for about 20 years. It applies for Unix/Linux.Daubigny
U
-2

That dropdown menu is on Spyder 5.1.5

Screnshot

Upheld answered 8/11, 2021 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.