Skip to content

Commit 86fa2a0

Browse files
committed
Make pylint respect libraries installed into VIRTUAL_ENV
Jedi respects VIRTUAL_ENV environment variable at finding out libraries. Therefore, (virtualenv) runtime for pyls/jedi can be separated from one for the target workspace. On the other hand, pylint does not respect VIRTUAL_ENV, and might cause unintentional "import-error" (E0401) for libraries installed in such virtualenv, even though jedi can recognize them. In order to make pylint respect libraries installed into VIRTUAL_ENV, this commit uses Document.sys_path() instead of sys.path of current pyls process, at spawning pylint. Document.sys_path() should respect VIRTUAL_ENV, (original) PYTHONPATH, and so on, because it uses Environment.get_sys_path() of jedi. This commit chooses changing pyls instead of pylint, because pylint uses "astroid" library to find out libraries imported in the target file, and making astroid respect VIRTUAL_ENV seems very difficult (at least, not so easy).
1 parent 2cc8080 commit 86fa2a0

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

pyls/plugins/pylint_lint.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,19 @@
1616

1717
if sys.version_info.major == 2:
1818
from StringIO import StringIO
19+
20+
# subprocess.Popen() on Windows expects that env contains only
21+
# "str" keys/values (= "bytes" for Python2.x, "unicode" for
22+
# Python3.x), even though pyls treats all path values as "unicode"
23+
# regardless of Python version
24+
def stringify(u):
25+
return u.encode('utf-8')
1926
else:
2027
from io import StringIO
2128

29+
def stringify(u):
30+
return u
31+
2232
log = logging.getLogger(__name__)
2333

2434

@@ -33,7 +43,7 @@ def spawn_pylint(document, flags):
3343
path = path.replace('\\', '/')
3444

3545
env = dict(os.environ)
36-
env["PYTHONPATH"] = os.pathsep.join(sys.path)
46+
env["PYTHONPATH"] = stringify(os.pathsep.join(document.sys_path()))
3747

3848
# Detect if we use Python as executable or not, else default to `python`
3949
executable = sys.executable if "python" in sys.executable else "python"

0 commit comments

Comments
 (0)