How to make an os.environ connection in python?

I have an os.environ configuration with default values โ€‹โ€‹(which cover 90% of my needs). I have a special application-framework-package, for example, called SALOME , which does not install the package in the system environment and tries to be standalone, it also requires the use of special old technologies that rely on environment variables, thus sys.path and PYTHONPATH - This is not the only thing he needs. I can get all the variables you need when it started calling os.environ inside the created environment. Then I can serialize this os.environ dictionary.

I wonder how to apply the os.environ merge on my current running system with the one I got through serialization?

+7
python environment-variables
source share
3 answers

Suppose you did the following to serialize your environment:

 import json import os with open('environ.json', 'w') as f: json.dump(dict(**os.environ), f) 

Now you can read them back (in another program)

 import json import os with open('environ.json', 'r') as f: os.environ.update(json.load(f)) 

This will add or change the current environment variables as stored, but any additional variables will remain.

If you want to update only certain variables by adding them (for example, to add additional paths), you can do this explicitly:

 with open('environ.json', 'r') as f: loadedenv = json.load(f) pathvars = ['PATH', 'PYTHONPATH'] for p in pathvars: os.environ[p] += ':' + loadedenv[p] 
+5
source share

You can use the environs package to achieve export of the os.environ dictionary. It has a built-in dump truck / loader to export the import of environment variables.

 from environs import Env env = Env() # reading an environment variable gh_user = env('GITHUB_USER') # => 'sloria' secret = env('SECRET') # => raises error if not set # casting api_key = env.str('API_KEY') # => '123abc' date = env.date('SHIP_DATE') # => datetime.date(1984, 6, 25) # serialize to a dictionary of simple types (numbers and strings) env.dump() # { 'API_KEY': '123abc', # 'GITHUB_USER': 'sloria', # 'SECRET': 'AASJI93WSJD93DWW3X0912NS2', # 'SHIP_DATE': '1984-06-25'}} 
+3
source share

If you want to have multiple values โ€‹โ€‹for a dictionary that the standard python dictionary does not offer, than you can use

 werkzeug.datastructures.MultiDict os.environ = MultiDict([('Key1', 'First Value'), ('Key1', 'Second Value')]) 

The update will also work the same as I mentioned below.

If you do not want to keep the old key values โ€‹โ€‹before merging with the new dictionary, you can do the following.

Since os.environ is a dictionary that you already have in memory, another dict - the one you are reading should be converted to json. I usually use ujson since it is really fast.

  os.environ.update(new_dict) 

If you want to save json u, you can send it to a file.

 import ujson with open('data.json', 'w') as file: ujson.dump(dict(**os.environ), file) 

if you want to read the file and update the os.environ dictionary that you can use.

 with open('environ.json', 'r') as f: os.environ.update(ujson.load(f)) 
+1
source share

All Articles