0

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!

Danilo Jakob
  • 71
  • 1
  • 6

1 Answers1

1

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

emmunaf
  • 426
  • 2
  • 9
  • Hi, im using Python 3.6 which comes with raspberry. I don't think this is MicroPython. But anyway using getenv and putenv didn't solve the problem. – Danilo Jakob Dec 21 '20 at 14:25
  • Oh I'm sorry, if so I'll check if any other idea comes to my mind. Sadly, I'm not able to reproduce the issue. Maybe [the answer provided here](https://raspberrypi.stackexchange.com/questions/71795/why-cant-my-python-script-find-a-custom-defined-environment-variable) can be helpful to you? – emmunaf Dec 21 '20 at 14:36
  • I added to the question the fact that the problem is on linux only. On windows this works perfect. Thats why I'm confused. But I'll go and check out the link you gave me. – Danilo Jakob Dec 21 '20 at 14:45
  • What happens if you use `environ['DISCOVERY_ENV'] = "test" print(environ.get('DISCOVERY_ENV'))` ? I mean, in the same .py script, just to check if there is an issue that's not on python side. For example it could happens that the first portion of your code and the second ones belongs to 2 different python files and they are executed in a different context. – emmunaf Dec 21 '20 at 14:46
  • The problem wasn't on the environ side, it was on how I tried to import the dependecies which don't work that way. Thank you very much for helping – Danilo Jakob Dec 21 '20 at 14:59