From 2ebab7cdaf60e5f7ea0ac24afa48ed2bc836b777 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 fc9e8cf7..c0b1341a 100644 --- a/isort/finders.py +++ b/isort/finders.py @@ -130,13 +130,8 @@ class PathFinder(BaseFinder): def __init__(self, config, sections): super(PathFinder, self).__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') @@ -155,6 +150,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') + 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: @@ -163,12 +169,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): 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: @@ -177,6 +189,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 bf6d93c9..29049ac6 100644 --- a/isort/main.py +++ b/isort/main.py @@ -278,6 +278,8 @@ def parse_args(argv=None): 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 From 35765c51ecb2a9e38bf2aa512caf42fe42170dd5 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Wed, 6 Mar 2019 20:20:25 -0800 Subject: Update changelog to inclde notice about 4.3.13 release --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3682f6cc..f7ebed58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ Changelog ========= +### 4.3.13 - March 6, 2019 - hot fix release +- Fixed the inability to accurately determine import section when a mix of conda and virtual environments are used. +- Fixed some output being printed even when --quiet mode is enabled. + ### 4.3.12 - March 6, 2019 - hot fix release - Fix error caused when virtual environment not detected -- cgit v1.2.1