我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用os.get_exec_path()。
def is_in_path(program): ''' Check if a program is in the system ``PATH``. Checks if a given program is in the user's ``PATH`` or not. Args: program (str): The program to try to find in ``PATH``. Returns: bool: Is the program in ``PATH``? ''' if sys.version_info.major == 2: path = os.getenv('PATH') if os.name == 'nt': path = path.split(';') else: path = path.split(':') else: path = os.get_exec_path() for i in path: if os.path.isdir(i): if program in os.listdir(i): return True
def test_get_exec_path(self): defpath_list = os.defpath.split(os.pathsep) test_path = ['/monty', '/python', '', '/flying/circus'] test_env = {'PATH': os.pathsep.join(test_path)} saved_environ = os.environ try: os.environ = dict(test_env) # Test that defaulting to os.environ works. self.assertSequenceEqual(test_path, os.get_exec_path()) self.assertSequenceEqual(test_path, os.get_exec_path(env=None)) finally: os.environ = saved_environ # No PATH environment variable self.assertSequenceEqual(defpath_list, os.get_exec_path({})) # Empty PATH environment variable self.assertSequenceEqual(('',), os.get_exec_path({'PATH':''})) # Supplied PATH environment variable self.assertSequenceEqual(test_path, os.get_exec_path(test_env)) if os.supports_bytes_environ: # env cannot contain 'PATH' and b'PATH' keys try: # ignore BytesWarning warning with warnings.catch_warnings(record=True): mixed_env = {'PATH': '1', b'PATH': b'2'} except BytesWarning: # mixed_env cannot be created with python -bb pass else: self.assertRaises(ValueError, os.get_exec_path, mixed_env) # bytes key and/or value self.assertSequenceEqual(os.get_exec_path({b'PATH': b'abc'}), ['abc']) self.assertSequenceEqual(os.get_exec_path({b'PATH': 'abc'}), ['abc']) self.assertSequenceEqual(os.get_exec_path({'PATH': b'abc'}), ['abc'])
def __get_python2_interpreter(self): paths = os.get_exec_path() for p in paths: for prog in ["python2", "python2.exe"]: attempt = os.path.join(p, prog) if os.path.isfile(attempt): return attempt return ""
def __init__( self, default_path: Iterable[PathLike] = os.get_exec_path() ) -> None: self.cache = {} # type: Dict[str, PathLike] self.search_path = None # type: Tuple[str, ...] self.reset_search_path(default_path)
def reset_search_path( self, default_path: Iterable[PathLike] = os.get_exec_path() ) -> None: """Reset the search path to `default_path`. Args: default_path: The default executable path. """ self.search_path = () if default_path: self.add_search_path(default_path)
def _test_internal_execvpe(self, test_type): program_path = os.sep + 'absolutepath' if test_type is bytes: program = b'executable' fullpath = os.path.join(os.fsencode(program_path), program) native_fullpath = fullpath arguments = [b'progname', 'arg1', 'arg2'] else: program = 'executable' arguments = ['progname', 'arg1', 'arg2'] fullpath = os.path.join(program_path, program) if os.name != "nt": native_fullpath = os.fsencode(fullpath) else: native_fullpath = fullpath env = {'spam': 'beans'} # test os._execvpe() with an absolute path with _execvpe_mockup() as calls: self.assertRaises(RuntimeError, os._execvpe, fullpath, arguments) self.assertEqual(len(calls), 1) self.assertEqual(calls[0], ('execv', fullpath, (arguments,))) # test os._execvpe() with a relative path: # os.get_exec_path() returns defpath with _execvpe_mockup(defpath=program_path) as calls: self.assertRaises(OSError, os._execvpe, program, arguments, env=env) self.assertEqual(len(calls), 1) self.assertSequenceEqual(calls[0], ('execve', native_fullpath, (arguments, env))) # test os._execvpe() with a relative path: # os.get_exec_path() reads the 'PATH' variable with _execvpe_mockup() as calls: env_path = env.copy() if test_type is bytes: env_path[b'PATH'] = program_path else: env_path['PATH'] = program_path self.assertRaises(OSError, os._execvpe, program, arguments, env=env_path) self.assertEqual(len(calls), 1) self.assertSequenceEqual(calls[0], ('execve', native_fullpath, (arguments, env_path)))