From 074d0d6d07d726442cad27004936fd0fd4297ce1 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sun, 4 Mar 2018 21:35:27 +0100 Subject: [PATCH] Include __init__.py files in search for the project directory, fixes #773 --- jedi/api/__init__.py | 4 +++- jedi/api/project.py | 14 +++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/jedi/api/__init__.py b/jedi/api/__init__.py index 9a05f060..e6ede6eb 100644 --- a/jedi/api/__init__.py +++ b/jedi/api/__init__.py @@ -100,7 +100,9 @@ class Script(object): sys_path = list(map(force_unicode, sys_path)) # Load the Python grammar of the current interpreter. - project = get_default_project(self.path or os.getcwd()) + project = get_default_project( + os.path.dirname(self.path)if path else os.getcwd() + ) # TODO deprecate and remove sys_path from the Script API. if sys_path is not None: project._sys_path = sys_path diff --git a/jedi/api/project.py b/jedi/api/project.py index e39a1a6a..622c8345 100644 --- a/jedi/api/project.py +++ b/jedi/api/project.py @@ -11,7 +11,7 @@ from jedi.evaluate.cache import evaluator_as_method_param_cache from jedi.common.utils import traverse_parents _CONFIG_FOLDER = '.jedi' -_CONTAINS_POTENTIAL_PROJECT = 'setup.py', '.git', '.hg', 'MANIFEST.in' +_CONTAINS_POTENTIAL_PROJECT = 'setup.py', '.git', '.hg', 'requirements.txt', 'MANIFEST.in' _SERIALIZER_VERSION = 1 @@ -171,12 +171,21 @@ def get_default_project(path=None): check = os.path.realpath(path) probable_path = None + first_no_init_file = None for dir in traverse_parents(check, include_current=True): try: return Project.load(dir) except (FileNotFoundError, NotADirectoryError): pass + if first_no_init_file is None: + if os.path.exists(os.path.join(dir, '__init__.py')): + # In the case that a __init__.py exists, it's in 99% just a + # Python package and the project sits at least one level above. + continue + else: + first_no_init_file = dir + if _is_django_path(dir): return Project(dir, _django=True) @@ -187,5 +196,8 @@ def get_default_project(path=None): # TODO search for setup.py etc return Project(probable_path) + if first_no_init_file is not None: + return Project(first_no_init_file) + curdir = path if os.path.isdir(path) else os.path.dirname(path) return Project(curdir)