Different getenv outputs depending on terminal used

Running some v basic scripts with environment variables so I don’t have to hard code things like my key or id. My code wouldn’t run and was failing on authorising so either my id and/or key didn’t work. So I just printed out the variables to see where it died.

import os
user = os.getenv('USER') 
tempLocation = os.getenv('TEMP') 
Id = os.getenv('MY_ID') 
Key = os.getenv('MY_KEY')

print(user) 
print(tempLocation) 
print(Id) 
print(Key)

But I’m getting different responses depending on where the code is run. My output is below, the id and key have been altered for here but they are returning info. From WSL terminal I’m only retuning my user name:

me 
None 
None 
None

But if I use the Powershell ( or in a Jupyter interactive terminal in VS Code) I get:

None 
C:\Users\me\AppData\Local\Temp 
xxxxxx 
1234567890abcdefg

I’d prefer to be able to just run the script from my WSL terminal, but Powershell will do. But mostly I’m confused as to why two different terminals for the same code on the same machine will produce different results.

Edit I could understand getting nothing in WSL if I’ve not got it set up correctly but if does at least get my username.

The question is not really about Python :slight_smile:

From the environment variables you are examining, only USER is set by default (and standardized) in most of Linux distros.

If you want to transfer some variables from Windows to WSL, there is a solution:

For example this will configure your profile in Windows to share USERPROFILE to WSL.

setx WSLENV USERPROFILE/up

Ah, sorry for the wrong place then! I thought it was the function, not my setup.

Thank you for the help.

But this is about Python :slight_smile:

You can run Jupyter inside WSL. Just pip install jupyterlab (ideally inside a venv) and start it inside WSL. Then you can open the localhost URL from the web browser in Windows.

Also VS Code is tightly integrated with WSL (or other remote development on Linux). Just enter your directory with your Python project and start VS Code from there: code . (the dot refers to the current directory).

You can also work with your Jupyter notebooks using your Linux instance of Jupyter from within VS Code. You can have everything (except VS Code) installed just inside WSL.

I am not sure if I understand this. From Linux (WSL) you can run your Python script this way:

python3 your_script.py

Do not add .exe at the end of the command. This would run the Windows installation of the program. If you do not need to use Python in Windows, you can uninstall it there and use it just inside WSL.

WSL and Windows are separate operating systems. Every has its own users, applications, environment variables…

To use your variables in Linux (WSL), set them in Linux:

export MY_ID=xxxxxx 
export MY_KEY=1234567890abcdefg
python3 your_script.py

Or for single-use:

MY_ID=xxxxxx MY_KEY=1234567890abcdefg python3 your_script.py

Sorry, I might not have been clear, no .exe was used. I’m running in WSL2 ubuntu ( lh example) and in Powershell (rh image) in the same way.
I thought getenv would call the same place regardless of which shell I ran it, if doesn’t that’s cool I can work around it and remember to run it from Powershell to use those variables. I think I wouldn’t have noticed if I’d tried Powershell first instead of WSL as my first script that needed it only called for a key and ID.

OK, I think all the important points have been said. Let’s summarize them.

  • Windows and Linux (running inside WSL) are different operating systems. As a simplification you can imagine them as two distinct computers with some interconnection between them.
  • By default what you set in one of them is not avalable in the other. (but you have some resources shared by default - like filesystems)

What to do? Two basic options:

  1. Use just one of the systems and set the variables there.
  2. Set the variables on both the systems - preferably from a single place:
    • using the WSLENV environment variable sharing mechanism I have shown to you
    • using a profile script…

BTW: It looks like you are using Windows Terminal - good choice. If you use PowerShell - do you know that you can run it inside Windows Terminal too? It is certainly much better than the old Windows Console.

Used this as a learning point and learned how to import variables from another file, which might not be the best method but for a very beginner, I’m pleased I worked out a solution.

I normally use Terminal for both Powershell and Bash, just want them next to each other for an easy picture! Many thanks for helping today/