def sysconfig2(): # import sysconfig module - Provide access to Python’s configuration information import sysconfig # returns an installation path corresponding to the path name print("Path Name : ", sysconfig.get_path("stdlib")) print() # returns a string that identifies the current platform. print("Current Platform : ", sysconfig.get_platform()) print() # returns the MAJOR.MINOR Python version number as a string print("Python Version Number : ", sysconfig.get_python_version()) print() # returns a tuple containing all path names print("Path Names : ", sysconfig.get_path_names()) print() # returns a tuple containing all schemes print("Scheme Names : ", sysconfig.get_scheme_names()) print() # returns the value of a single variable name. print("Variable name LIBDIR : ", sysconfig.get_config_var('LIBDIR')) # returns the value of a single variable name. print("Variable name LIBDEST : ", sysconfig.get_config_var('LIBDEST'))
def _get_default_library_roots(cls): pydev_log.debug("Collecting default library roots.") # Provide sensible defaults if not in env vars. import site roots = [] try: import sysconfig # Python 2.7 onwards only. except ImportError: pass else: for path_name in set( ('stdlib', 'platstdlib', 'purelib', 'platlib')) & set( sysconfig.get_path_names()): roots.append(sysconfig.get_path(path_name)) # Make sure we always get at least the standard library location (based on the `os` and # `threading` modules -- it's a bit weird that it may be different on the ci, but it happens). roots.append(os.path.dirname(os.__file__)) roots.append(os.path.dirname(threading.__file__)) if IS_PYPY: # On PyPy 3.6 (7.3.1) it wrongly says that sysconfig.get_path('stdlib') is # <install>/lib-pypy when the installed version is <install>/lib_pypy. try: import _pypy_wait except ImportError: pydev_log.debug( "Unable to import _pypy_wait on PyPy when collecting default library roots." ) else: pypy_lib_dir = os.path.dirname(_pypy_wait.__file__) pydev_log.debug("Adding %s to default library roots.", pypy_lib_dir) roots.append(pypy_lib_dir) if hasattr(site, 'getusersitepackages'): site_paths = site.getusersitepackages() if isinstance(site_paths, (list, tuple)): for site_path in site_paths: roots.append(site_path) else: roots.append(site_paths) if hasattr(site, 'getsitepackages'): site_paths = site.getsitepackages() if isinstance(site_paths, (list, tuple)): for site_path in site_paths: roots.append(site_path) else: roots.append(site_paths) for path in sys.path: if os.path.exists(path) and os.path.basename( path) == 'site-packages': roots.append(path) roots.extend([os.path.realpath(path) for path in roots]) return sorted(set(roots))
def pytest_report_header(config): try: import multiprocessing except ImportError: pass else: print('Number of processors: %s' % (multiprocessing.cpu_count(), )) import sysconfig from os.path import realpath print('Relevant system paths:') print('sys.prefix: %s (%s)' % (sys.prefix, realpath(sys.prefix))) if hasattr(sys, 'base_prefix'): print('sys.base_prefix: %s (%s)' % (sys.base_prefix, realpath(sys.base_prefix))) if hasattr(sys, 'real_prefix'): print('sys.real_prefix: %s (%s)' % (sys.real_prefix, realpath(sys.real_prefix))) if hasattr(site, 'getusersitepackages'): paths = site.getusersitepackages() if isinstance(paths, (list, tuple)): real_paths = list(realpath(p) for p in paths) else: real_paths = realpath(paths) print('site.getusersitepackages(): %s (%s)' % (site.getusersitepackages(), real_paths)) if hasattr(site, 'getsitepackages'): paths = site.getsitepackages() if isinstance(paths, (list, tuple)): real_paths = list(realpath(p) for p in paths) else: real_paths = realpath(paths) print('site.getsitepackages(): %s (%s)' % (site.getsitepackages(), real_paths)) for path in sys.path: if os.path.exists(path) and os.path.basename(path) == 'site-packages': print('Folder with "site-packages" in sys.path: %s (%s)' % (path, realpath(path))) for path_name in sorted(sysconfig.get_path_names()): print('sysconfig: %s: %s (%s)' % (path_name, sysconfig.get_path(path_name), realpath(sysconfig.get_path(path_name)))) print( 'os module dir: %s (%s)' % (os.path.dirname(os.__file__), realpath(os.path.dirname(os.__file__)))) print('threading module dir: %s (%s)' % (os.path.dirname( threading.__file__), realpath(os.path.dirname(threading.__file__))))
def get_python_include_paths(): ans = [] for name in sysconfig.get_path_names(): if 'include' in name: ans.append(name) def gp(x): return sysconfig.get_path(x) return sorted(frozenset(filter(None, map(gp, sorted(ans)))))
def sysconfig_path(self, state, args, kwargs): if len(args) != 1: raise mesonlib.MesonException('sysconfig_path() requires passing the name of path to get.') path_name = args[0] valid_names = sysconfig.get_path_names() if path_name not in valid_names: raise mesonlib.MesonException(f'{path_name} is not a valid path name {valid_names}.') # Get a relative path without a prefix, e.g. lib/python3.6/site-packages return sysconfig.get_path(path_name, vars={'base': '', 'platbase': '', 'installed_base': ''})[1:]
def sysconfig_path(self, state, args, kwargs): if len(args) != 1: raise mesonlib.MesonException('sysconfig_path() requires passing the name of path to get.') path_name = args[0] valid_names = sysconfig.get_path_names() if path_name not in valid_names: raise mesonlib.MesonException('{} is not a valid path name {}.'.format(path_name, valid_names)) # Get a relative path without a prefix, e.g. lib/python3.6/site-packages path = sysconfig.get_path(path_name, vars={'base': '', 'platbase': '', 'installed_base': ''})[1:] return ModuleReturnValue(path, [])
def sysconfig_path(self, state, args, kwargs): if len(args) != 1: raise mesonlib.MesonException( 'sysconfig_path() requires passing the name of path to get.') path_name = args[0] valid_names = sysconfig.get_path_names() if path_name not in valid_names: raise mesonlib.MesonException( '{} is not a valid path name {}.'.format( path_name, valid_names)) # Get a relative path without a prefix, e.g. lib/python3.6/site-packages path = sysconfig.get_path(path_name, vars={'base': ''})[1:] return ModuleReturnValue(path, [])
def _get_default_library_roots(cls): # Provide sensible defaults if not in env vars. import site roots = [] try: import sysconfig # Python 2.7 onwards only. except ImportError: pass else: for path_name in set( ('stdlib', 'platstdlib', 'purelib', 'platlib')) & set( sysconfig.get_path_names()): roots.append(sysconfig.get_path(path_name)) # Make sure we always get at least the standard library location (based on the `os` and # `threading` modules -- it's a bit weird that it may be different on the ci, but it happens). roots.append(os.path.dirname(os.__file__)) roots.append(os.path.dirname(threading.__file__)) if hasattr(site, 'getusersitepackages'): site_paths = site.getusersitepackages() if isinstance(site_paths, (list, tuple)): for site_path in site_paths: roots.append(site_path) else: roots.append(site_paths) if hasattr(site, 'getsitepackages'): site_paths = site.getsitepackages() if isinstance(site_paths, (list, tuple)): for site_path in site_paths: roots.append(site_path) else: roots.append(site_paths) for path in sys.path: if os.path.exists(path) and os.path.basename( path) == 'site-packages': roots.append(path) roots.extend([os.path.realpath(path) for path in roots]) return sorted(set(roots))
# coding=utf-8 # 使用sysconfig import sysconfig print sysconfig.get_config_var('Py_ENABLE_SHARED') print sysconfig.get_config_var('LIBDIR') print sysconfig.get_config_vars('AR', "CXX") print sysconfig.get_scheme_names() print sysconfig.get_path_names() print sysconfig.get_python_version() print sysconfig.get_platform() # return true if current python installation was built from source print sysconfig.is_python_build() print sysconfig.get_config_h_filename() print sysconfig._get_makefile_filename()
def describe_environment(header): import multiprocessing import sysconfig import site # noqa result = [header, "\n\n"] def report(*args, **kwargs): result.append(fmt(*args, **kwargs)) def report_paths(get_paths, label=None): prefix = fmt(" {0}: ", label or get_paths) expr = None if not callable(get_paths): expr = get_paths get_paths = lambda: util.evaluate(expr) try: paths = get_paths() except AttributeError: report("{0}<missing>\n", prefix) return except Exception: exception( "Error evaluating {0}", repr(expr) if expr else compat.srcnameof(get_paths), ) return if not isinstance(paths, (list, tuple)): paths = [paths] for p in sorted(paths): report("{0}{1}", prefix, p) rp = os.path.realpath(p) if p != rp: report("({0})", rp) report("\n") prefix = " " * len(prefix) report("CPU count: {0}\n\n", multiprocessing.cpu_count()) report("System paths:\n") report_paths("sys.prefix") report_paths("sys.base_prefix") report_paths("sys.real_prefix") report_paths("site.getsitepackages()") report_paths("site.getusersitepackages()") site_packages = [ p for p in sys.path if os.path.exists(p) and os.path.basename(p) == "site-packages" ] report_paths(lambda: site_packages, "sys.path (site-packages)") for name in sysconfig.get_path_names(): expr = fmt("sysconfig.get_path({0!r})", name) report_paths(expr) report_paths("os.__file__") report_paths("threading.__file__") result = "".join(result).rstrip("\n") info("{0}", result)
def _get_current_info(): is_venv = (sys.prefix != sys.base_prefix) base_executable = getattr(sys, '_base_executable', None) if is_venv: # XXX There is probably a bug related to venv, since # sys._base_executable should be different. if base_executable == sys.executable: # Indicate that we don't know. base_executable = None elif not base_executable: base_executable = sys.executable info = { # locations 'executable (sys)': sys.executable, 'executable (sys;realpath)': os.path.realpath(sys.executable), 'prefix (sys)': sys.prefix, 'exec_prefix (sys)': sys.exec_prefix, 'stdlib_dir': os.path.dirname(os.__file__), 'stdlib_dir (sys)': getattr(sys, '_stdlib_dir', None), 'stdlib_dir (sysconfig)': (sysconfig.get_path('stdlib') if 'stdlib' in sysconfig.get_path_names() else None), # base locations 'base_executable': base_executable, 'base_executable (sys)': getattr(sys, '_base_executable', None), 'base_prefix (sys)': sys.base_prefix, 'base_exec_prefix (sys)': sys.base_exec_prefix, # version 'version_str (sys)': sys.version, 'version_info (sys)': sys.version_info, 'hexversion (sys)': sys.hexversion, 'api_version (sys)': sys.api_version, # implementation 'implementation_name (sys)': sys.implementation.name, 'implementation_version (sys)': sys.implementation.version, # build 'is_dev (sysconfig)': sysconfig.is_python_build(), # host 'platform (sys)': sys.platform, # virtual envs 'is_venv': is_venv, # import system # importlib.util.MAGIC_NUMBER has been around since 3.5. 'pyc_magic_number': importlib.util.MAGIC_NUMBER, } return info
def test_get_path_names(self): self.assertEqual(get_path_names(), sysconfig._SCHEME_KEYS)
#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """The names of the paths in a scheme. """ #end_pymotw_header import sysconfig for name in sysconfig.get_path_names(): print name
def get_python_include_paths(): ans = [] for name in sysconfig.get_path_names(): if 'include' in name: ans.append(name) return sorted(frozenset(map(sysconfig.get_path, sorted(ans))))
def update_event(self, inp=-1): self.set_output_val(0, sysconfig.get_path_names())
print(sysconfig.get_config_var('LIBDIR')) print(sysconfig.get_config_vars('posix_home', 'prefix')) print(sysconfig.get_config_vars()) print(sysconfig.get_scheme_names()) """ stdlib: directory containing the standard Python library files that are not platform-specific. platstdlib: directory containing the standard Python library files that are platform-specific. platlib: directory for site-specific, platform-specific files. purelib: directory for site-specific, non-platform-specific files. include: directory for non-platform-specific header files. platinclude: directory for platform-specific header files. scripts: directory for script files. data: directory for data files. """ print(sysconfig.get_path_names()) print(sysconfig.get_path("data")) print(sysconfig.get_paths("nt")) print(sysconfig.get_python_version()) print(sysconfig.get_platform()) print(sysconfig.is_python_build()) print(sysconfig.get_config_h_filename()) print(sysconfig.get_makefile_filename())
#!/usr/bin/env python3 # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """The names of the paths in a scheme. """ #end_pymotw_header import sysconfig for name in sysconfig.get_path_names(): print(name)
def warn_on_path_overlap(): """ Importing modules under multiple sys.modules names (e.g. borrow, apps.borrow) is a terrible python wart. It causes many subtle bugs. This function will issue a logged error if it finds any modules with duplicate keys in sys.modules (based on matching paths). It takes a bit to run, but will save your sanity. It's only useful once duplicates are loaded, so make sure to exercise your imports before running. """ ignored_paths = set() # Ignore stdlib paths if sysconfig is not None: for path_name in sysconfig.get_path_names(): ignored_paths.add(sysconfig.get_path(path_name)) def is_ignored_path(module_path): for path in ignored_paths: # FIXME: this excludes user packages in site-packages because it is a subdir of stdlib. # Fix that. if os.path.commonprefix([path, module_path]) == path: return True return False ignored_modules = set( [ # Yes, the email stdlib packages really does alias all these. "email.Header", "email.header", "email.FeedParser", "email.feedparser", "email.MIMEMessage", "email.mime.message", "email.MIMEBase", "email.mime.base", "email.MIMEImage", "email.mime.image", "email.MIMEText", "email.mime.text", "email.Message", "email.message", "email.Errors", "email.errors", "email.MIMEMultipart", "email.mime.multipart", "email.quopriMIME", "email.quoprimime", "email.MIMENonMultipart", "email.mime.nonmultipart", "email.Parser", "email.parser", "email.Generator", "email.generator", "email.MIMEAudio", "email.mime.audio", "email.Iterators", "email.iterators", "email.base64MIME", "email.base64mime", "email.Charset", "email.charset", "email.Utils", "email.utils", "email.Encoders", "email.encoders" ]) def is_ignored_module(module_key): return module_key in ignored_modules def get_module_path(module_key): module = sys.modules[module_key] if module is None: # Seems to happen on builtins... return None if not hasattr(module, '__file__'): # Happens on C modules return None return module.__file__ seen_comparisons = set() for outer_key in sys.modules.keys(): if is_ignored_module(outer_key): continue outer_path = get_module_path(outer_key) if outer_path is None: continue if is_ignored_path(outer_path): continue for inner_key in sys.modules.keys(): if outer_key == inner_key: # Skip if the module is the very same. continue comparison = tuple(sorted([outer_key, inner_key])) # Avoid listing twice. if comparison in seen_comparisons: continue else: seen_comparisons.add(comparison) inner_path = get_module_path(inner_key) if inner_path is None: continue if inner_path == outer_path: logging.error( "Differently-named modules have the same implementation: %s, %s; Fix your sys.path to not cause duplicate paths to packages, and avoid relative imports.", outer_key, inner_key)
def test_get_path_names(self): self.assertEqual(get_path_names(), _SCHEMES.options('posix_prefix'))
def __init__(self): def u(v): return v.decode("utf-8") if isinstance(v, bytes) else v def abs_path(v): return None if v is None else os.path.abspath( v) # unroll relative elements from path (e.g. ..) # qualifies the python self.platform = u(sys.platform) self.implementation = u(platform.python_implementation()) if self.implementation == "PyPy": self.pypy_version_info = tuple(u(i) for i in sys.pypy_version_info) # this is a tuple in earlier, struct later, unify to our own named tuple self.version_info = VersionInfo(*list(u(i) for i in sys.version_info)) self.architecture = 64 if sys.maxsize > 2**32 else 32 self.version = u(sys.version) self.os = u(os.name) # information about the prefix - determines python home self.prefix = u(abs_path(getattr(sys, "prefix", None))) # prefix we think self.base_prefix = u(abs_path(getattr(sys, "base_prefix", None))) # venv self.real_prefix = u(abs_path(getattr(sys, "real_prefix", None))) # old virtualenv # information about the exec prefix - dynamic stdlib modules self.base_exec_prefix = u( abs_path(getattr(sys, "base_exec_prefix", None))) self.exec_prefix = u(abs_path(getattr(sys, "exec_prefix", None))) self.executable = u(abs_path( sys.executable)) # the executable we were invoked via self.original_executable = u(abs_path( self.executable)) # the executable as known by the interpreter self.system_executable = self._fast_get_system_executable( ) # the executable we are based of (if available) try: __import__("venv") has = True except ImportError: has = False self.has_venv = has self.path = [u(i) for i in sys.path] self.file_system_encoding = u(sys.getfilesystemencoding()) self.stdout_encoding = u(getattr(sys.stdout, "encoding", None)) self.sysconfig_paths = { u(i): u(sysconfig.get_path(i, expand=False)) for i in sysconfig.get_path_names() } config_var_keys = set() for element in self.sysconfig_paths.values(): for k in _CONF_VAR_RE.findall(element): config_var_keys.add(u(k[1:-1])) config_var_keys.add("PYTHONFRAMEWORK") self.sysconfig_vars = { u(i): u(sysconfig.get_config_var(i) or "") for i in config_var_keys } if self.implementation == "PyPy" and sys.version_info.major == 2: self.sysconfig_vars[u"implementation_lower"] = u"python" self.distutils_install = { u(k): u(v) for k, v in self._distutils_install().items() } confs = { k: (self.system_prefix if v.startswith(self.prefix) else v) for k, v in self.sysconfig_vars.items() } self.system_stdlib = self.sysconfig_path("stdlib", confs) self.system_stdlib_platform = self.sysconfig_path("platstdlib", confs) self.max_size = getattr(sys, "maxsize", getattr(sys, "maxint", None)) self._creators = None
def setup_connection(): ptvsd.log.debug('sys.prefix: {0}', (sys.prefix, )) if hasattr(sys, 'base_prefix'): ptvsd.log.debug('sys.base_prefix: {0}', sys.base_prefix) if hasattr(sys, 'real_prefix'): ptvsd.log.debug('sys.real_prefix: {0}', sys.real_prefix) if hasattr(site, 'getusersitepackages'): ptvsd.log.debug('site.getusersitepackages(): {0}', site.getusersitepackages()) if hasattr(site, 'getsitepackages'): ptvsd.log.debug('site.getsitepackages(): {0}', site.getsitepackages()) for path in sys.path: if os.path.exists(path) and os.path.basename(path) == 'site-packages': ptvsd.log.debug('Folder with "site-packages" in sys.path: {0}', path) for path_name in {'stdlib', 'platstdlib', 'purelib', 'platlib'} & set( sysconfig.get_path_names()): ptvsd.log.debug('sysconfig {0}: {1}', path_name, sysconfig.get_path(path_name)) ptvsd.log.debug('os dir: {0}', os.path.dirname(os.__file__)) ptvsd.log.debug('threading dir: {0}', os.path.dirname(threading.__file__)) opts = ptvsd.options pydevd.apply_debugger_options({ 'server': not opts.client, 'client': opts.host, 'port': opts.port, 'multiprocess': opts.multiprocess, }) if opts.multiprocess: listen_for_subprocesses() # We need to set up sys.argv[0] before invoking attach() or enable_attach(), # because they use it to report the 'process' event. Thus, we can't rely on # run_path() and run_module() doing that, even though they will eventually. if opts.target_kind == 'code': sys.argv[0] = '-c' elif opts.target_kind == 'file': sys.argv[0] = opts.target elif opts.target_kind == 'module': # Add current directory to path, like Python itself does for -m. This must # be in place before trying to use find_spec below to resolve submodules. sys.path.insert(0, '') # We want to do the same thing that run_module() would do here, without # actually invoking it. On Python 3, it's exposed as a public API, but # on Python 2, we have to invoke a private function in runpy for this. # Either way, if it fails to resolve for any reason, just leave argv as is. try: if sys.version_info >= (3, ): from importlib.util import find_spec spec = find_spec(opts.target) if spec is not None: sys.argv[0] = spec.origin else: _, _, _, sys.argv[0] = runpy._get_module_details(opts.target) except Exception: ptvsd.log.exception('Error determining module path for sys.argv') else: assert False ptvsd.log.debug('sys.argv after patching: {0!r}', sys.argv) addr = (opts.host, opts.port) global daemon if opts.no_debug: daemon = ptvsd.runner.Daemon() if not daemon.wait_for_launch(addr): return elif opts.client: daemon = ptvsd._remote.attach(addr) else: daemon = ptvsd._remote.enable_attach(addr) if opts.wait: ptvsd.wait_for_attach()