7 examples of 'python check if environment variable is set' in Python

Every line of 'python check if environment variable is set' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
22def environ_path_variable_exists(variable_name):
23 """Determines if the os.environ variable exists and is a valid path.
24
25 :rtype: bool
26 """
27 try:
28 return os.path.exists(os.environ[variable_name])
29 except KeyError:
30 return False
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

65def check_environment_variables():
66 print("CHECKING ENVIRONMENT VARIABLES")
67 valid_env_file = all(
68 check_keys(all_keys_to_check[element], element) for element in all_keys_to_check
69 )
70
71 if not valid_env_file:
72 print_env_file_warning()
73
74 return valid_env_file
159def setGlobalEnvWin(key, value):
160 """Set a global environment variable on Windows"""
161 if not key:
162 return
163
164 if not value:
165 value = '' # this will clear value
166
167 s = 'setx %s "%s"' % (key, value)
168 output = runCommand(s)
169 logging.info('setx returned: %s', output)
170
171 return True
34def verify_environ():
35 to_have = ['AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'AWS_BUCKET_NAME']
36 error = False
37 for key in to_have:
38 if key not in os.environ:
39 error = True
40 print('Must define ' + key + ' to upload to S3')
41 if 'NEXT_BACKEND_GLOBAL_HOST' not in os.environ:
42 print('NEXT_BACKEND_GLOBAL_HOST is not defined, '
43 'defaulting to localhost')
44 return error
142def checkenv(args):
143 env = {}
144
145 # database
146 cmd = "dropdb"
147 env[cmd] = search_cmd(cmd)
148
149 cmd = "createdb"
150 env[cmd] = search_cmd(cmd)
151
152 # pdal stuff
153 cmd = "pdal"
154 env[cmd] = search_cmd(cmd)
155
156 cmd = "pdal-config"
157 env[cmd] = search_cmd(cmd)
158
159 out = run_cmd([env["pdal-config"], "--plugin-dir"])
160 pdal_plugindir = str(out, 'utf-8').replace('\n', '')
161 midoc = os.path.join(pdal_plugindir, "libpdal_plugin_filter_midoc.so")
162 search_lib(midoc, "pdal plugin midoc filter")
163
164 pgpointcloud_writer = os.path.join(pdal_plugindir, "libpdal_plugin_writer_pgpointcloud.so")
165 search_lib(pgpointcloud_writer, "pdal plugin pgpointcloud writer")
166
167 return env
162@contextmanager
163def ensure_python_environment_preserved():
164 freeze = subprocess.check_output("{} -m pip freeze".format(sys.executable), stderr=subprocess.STDOUT, shell=True).decode()
165 try:
166 yield
167 finally:
168 freeze_after = subprocess.check_output("{} -m pip freeze".format(sys.executable), stderr=subprocess.STDOUT, shell=True).decode()
169 if freeze != freeze_after:
170 writeln_console(">>> " + colorama.Fore.RED + "This example modifies the Python dependencies!")
171 removed = set(freeze.splitlines()) - set(freeze_after.splitlines())
172 added = set(freeze_after.splitlines()) - set(freeze.splitlines())
173 for it in removed:
174 writeln_console("- " + it)
175 for it in added:
176 writeln_console("+ " + it)
177 raise Exception("Example modifies Python environment!")
142def exists(env):
143 for version in versions:
144 if get_winddk_root(env, version) is not None:
145 return True
146 return False

Related snippets