From 9d0c4800b947c5928c8e64f2dd3173b9a5631734 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Wed, 6 Mar 2019 20:19:56 -0800 Subject: Implement conda environment support --- isort/finders.py | 28 +++++++++++++++++++++------- isort/main.py | 2 ++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/isort/finders.py b/isort/finders.py index dcfaf8a6..75fe79a3 100644 --- a/isort/finders.py +++ b/isort/finders.py @@ -118,13 +118,8 @@ class PathFinder(BaseFinder): def __init__(self, config: Mapping[str, Any], sections: Any) -> None: super().__init__(config, sections) - # Use a copy of sys.path to avoid any unintended modifications - # to it - e.g. `+=` used below will change paths in place and - # if not copied, consequently sys.path, which will grow unbounded - # with duplicates on every call to this method. - self.paths = list(sys.path) # restore the original import path (i.e. not the path to bin/isort) - self.paths[0] = os.getcwd() + self.paths = [os.getcwd()] # virtual env self.virtual_env = self.config.get('virtual_env') or os.environ.get('VIRTUAL_ENV') @@ -143,6 +138,17 @@ class PathFinder(BaseFinder): if os.path.isdir(path): self.paths.append(path) + # conda + self.conda_env = self.config.get('conda_env') or os.environ.get('CONDA_PREFIX') or '' + if self.conda_env: + self.conda_env = os.path.realpath(self.conda_env) + for path in glob('{0}/lib/python*/site-packages'.format(self.conda_env)): + if path not in self.paths: + self.paths.append(path) + for path in glob('{0}/lib/python*/*/site-packages'.format(self.conda_env)): + if path not in self.paths: + self.paths.append(path) + # handle case-insensitive paths on windows self.stdlib_lib_prefix = os.path.normcase(sysconfig.get_paths()['stdlib']) if self.stdlib_lib_prefix not in self.paths: @@ -151,12 +157,18 @@ class PathFinder(BaseFinder): # handle compiled libraries self.ext_suffix = sysconfig.get_config_var("EXT_SUFFIX") or ".so" + # add system paths + for path in sys.path[1:]: + if path not in self.paths: + self.paths.append(path) + def find(self, module_name: str) -> Optional[str]: for prefix in self.paths: package_path = "/".join((prefix, module_name.split(".")[0])) is_module = (exists_case_sensitive(package_path + ".py") or exists_case_sensitive(package_path + ".so") or - exists_case_sensitive(package_path + self.ext_suffix)) + exists_case_sensitive(package_path + self.ext_suffix) or + exists_case_sensitive(package_path + "/__init__.py")) is_package = exists_case_sensitive(package_path) and os.path.isdir(package_path) if is_module or is_package: if 'site-packages' in prefix: @@ -165,6 +177,8 @@ class PathFinder(BaseFinder): return self.sections.THIRDPARTY if self.virtual_env and self.virtual_env_src in prefix: return self.sections.THIRDPARTY + if self.conda_env and self.conda_env in prefix: + return self.sections.THIRDPARTY if os.path.normcase(prefix).startswith(self.stdlib_lib_prefix): return self.sections.STDLIB return self.config['default_section'] diff --git a/isort/main.py b/isort/main.py index 8842a318..1eea5b12 100644 --- a/isort/main.py +++ b/isort/main.py @@ -277,6 +277,8 @@ def parse_args(argv: Optional[Sequence[str]] = None) -> Dict[str, Any]: help='Shows verbose output, such as when files are skipped or when a check is successful.') parser.add_argument('--virtual-env', dest='virtual_env', help='Virtual environment to use for determining whether a package is third-party') + parser.add_argument('--conda-env', dest='conda_env', + help='Conda environment to use for determining whether a package is third-party') parser.add_argument('-vn', '--version-number', action='version', version=__version__, help='Returns just the current version number without the logo') parser.add_argument('-w', '--line-width', help='The max length of an import line (used for wrapping long imports).', -- cgit v1.2.1