0

A Python script is used on different platforms - Cygwin on Windows 10 and Linux on a Raspberry Pi. Some script parameters depend on the platform, so I am trying to read the value of the OSTYPE environment variable. Although it is defined and readable from the command line, it is not defined in the script context.

Consider the following (copied from the Cygwin terminal, but is similar on the RPi terminal):

$ echo $TEMP
/tmp

$ echo $OSTYPE
cygwin

$ py3
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ.get('TEMP')
'C:\\cygwin64\\tmp'
>>> os.environ.get('OSTYPE')
>>> os.getenv('OSTYPE')
>>>

I also tryied export-ing the variable, but got the same result.

Why is the OSTYPE variable missing from Python's environment?

ysap
  • 7,723
  • 7
  • 59
  • 122
  • 1
    It seems that `OSTYPE` isn’t an environment variable (instead it’s a regular, un-exported variable). It being defined in the command line doesn’t show that it has been exported into the environment. How did you try exporting it? – Konrad Rudolph Jan 12 '22 at 19:13
  • @KonradRudolph - I am not sure I understand the difference. When I type `set`, I see the variable. Also, as mentioned in the question, I tried exporting it but it did not change the outcome. – ysap Jan 12 '22 at 19:15
  • `declare -p OSTYPE` and see if it says `declare -- OSTYPE='...'` (regular shell variable or `declare -x OSTYPE='...'` (environment variable) – Charles Duffy Jan 12 '22 at 19:17
  • Again: *how* did you try exporting it? You probably made a mistake when doing so. – Konrad Rudolph Jan 12 '22 at 19:17
  • Only _exported_ variables, aka environment variables (with the `-x` flag) survive an `execv()` syscall; other shell variables are stored in the heap and are overwritten with the new process image. `echo $OSTYPE` doesn't tell us which type of variable it is (and is also buggy in other ways; see [I just assigned a variable, but `echo $variable` shows something else!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else)). – Charles Duffy Jan 12 '22 at 19:17
  • I don't know where it is defined. I assumed it is defined by the shell, but I may be wrong. Also, this page suggests that it *is* a shell variable: https://tldp.org/LDP/abs/html/internalvariables.html Maybe we use a different meaning to "environment"? – ysap Jan 12 '22 at 19:19
  • The ABS is a horrible, habitually-broken excuse for reference material -- full of bad-practice examples and outdated information. Do not use it, at all, for anything. The [BashGuide](https://mywiki.wooledge.org/BashGuide) was written _explicitly_ to have a reference without all the ABS's misinformation. – Charles Duffy Jan 12 '22 at 19:20
  • 1
    Anyhow -- all preexisting environment variables that have names that are also valid shell variable names _become_ shell variables during shell startup while still also remaining environment variables, but not all shell variables are environment variables. So showing us that you have an `OSTYPE` _shell_ variable does not show us that you have an `OSTYPE` _environment_ variable, and only environment variables are expected to be visible in subprocesses. You need to `export OSTYPE` or `declare -x OSTYPE` to force a shell variable to become an environment variable. – Charles Duffy Jan 12 '22 at 19:21
  • @CharlesDuffy - thanks. As noted in the question, I did try `export OSTYPE` and it did not work. But getting back to that, I noticed I had an error (as suggested above by @Konrad), and once exported correctly, I can now see the variable in Python. – ysap Jan 12 '22 at 19:35

0 Answers0