jmcantrell/vim-virtualenv

PYTHONPATH mangling breaks `!supervisorctl`

Closed this issue · 5 comments

The change from #15 breaks invoking supervisorctl for me.

:!supervisorctl update fails as follows:

Traceback (most recent call last):
  File "/home/user/.virtualenvs/project/bin/supervisorctl", line 9, in <module>
    load_entry_point('supervisor==3.0', 'console_scripts', 'supervisorctl')()
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/pkg_resources.py", line 378, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/pkg_resources.py", line 2566, in load_entry_point
    return ep.load()
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/pkg_resources.py", line 2260, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
ImportError: No module named supervisor.supervisorctl

shell returned 1

Press ENTER or type command to continue

This does not happen when I remove the addition of sys.path to PYTHONPATH.

The reason for this is that /usr/lib/python2.7 gets added to PYTHONPATH. Removing it from the huge list (1394 chars), fixes it.

Apart from that, I think that copying the whole of sys.path into PYTHONPATH should be avoided.

I do not know which problem(s) @mtth tried to fix here, but it might just be necessary to install packages also inside of the virtualenv.

I will disable the PYTHONPATH mangling locally and see if I run into any problems.

@mtth: can you please elaborate on the intention behind this? After all the virtualenv activation scripts do not touch PYTHONPATH either.

mtth commented

@blueyed -

The motivation behind the initial pull request is to allow other command line utilities to access modules in your virtual environment (e.g. pylint). You can take a look here for more information. Note that, as you mention, installing each of these utilities separately in each virtual environment would also fix this, but having multiple installations of generic tools such as pylint seems unnecessary and cumbersome.

I'm unable to reproduce your error. I did a fresh install of supervisor outside any virtual environment and supervisorctl update works fine from inside Vim even after activating one. Which versions of python are you using (inside and outside the virtual environment)? Where is supervisorctl installed?

Something seems strange as $PYTHONPATH and sys.path are actually very similar (doc). Namely, copying sys.path into $PYTHONPATH would only add exactly what the activate_this.py script is adding (you can check that sys.path does deduplication for you). That is, unless you are using a different version of python between your virtual environment and where supervisorctl is installed.

@mtth

I have installed supervisor inside of the virtualenv (.virtualenvs/tmm/lib/python2.7/site-packages/supervisor).
I am using pyenv, with Python 2.7.6 installed.
Vim however is built against the system's Python!

It seems that the PYTHONPATH mangling picks up the search path from Vim's python (2.7.5+), and then supervisorctl is invoked using pyenv's Python (2.7.6).
What I don't get though, is why the insertion/injection of /usr/lib/python2.7 (from Vim's / the system's Python) causes/triggers the ImportError. Shouldn't it also look in the virtualenv's site-packages?

To complicate things, the virtualenv has been installed using the system's/Vim's python initially.

The main problem however appears to be that sys.path from Vim is different from sys.path of the virtualenv.

PYTHONPATH mangling also breaks python3 virtualenvs.
The resulting PYTHONPATH looks like
/home/martink/.virtualenvs/test1/lib/python2.7/site-packages:/usr/lib/python2.7:...
which is obviously wrong for python3 environment. As a result commands depending on python3 are broken, e.g.:
:!pylint --version

Fatal Python error: Py_Initialize: Unable to get the locale encoding
  File "/usr/lib/python2.7/encodings/__init__.py", line 123
    raise CodecRegistryError,\
                            ^
SyntaxError: invalid syntax

Command terminated

This breaks e.g. syntastic as it is not able to launch checkers for python3.
Commenting out the PYTHONPATH mangling solves the issue.

@mkoura, I'm not surprised. I haven't made any changes for python3 yet, as I still use 2.7 with vim.

Is this still an issue?