pythonenviron

Python doesn't set Environment variables


I've written a python application which can be set in development or production mode by using an environment variable. This variable can be passed as a CLI argument:

    if len(argv) >= 2:
        environ['DISCOVERY_ENV'] = argv[1]
    else:
        environ['DISCOVERY_ENV'] = 'development'

The problem now is that this environment is not being set. Which means if I've following code, it doesn't work:

if environ.get('DISCOVERY_ENV') == 'production':
    import adafruit_ads1x15.ads1015 as ADS
    from adafruit_ads1x15.analog_in import AnalogIn
    import busio
    import board

In this example the libraries will not being imported.

EDIT:

This does work on Windows but not on linux, my case: Rasbian OS.

Does anyone know why this doesn't work?

Thanks in advance!


Solution

  • By looking at your code, I think you are on micropython. If this is the case, according to the doc, environ is not implemented yet.

    By quoting the doc, the suggestion seems to be the following:

    Workaround: Use getenv, putenv and unsetenv