Wee-slack: No module named websocket using pyenv

Created on 25 Mar 2018  ยท  11Comments  ยท  Source: wee-slack/wee-slack

OS X High Sierra, all programs brew installed, e.g. brew install weechat --with-aspell --with-lua --with-perl --with-python@2 --with-ruby.

> pyenv virtualenv weechat
> pyenv shell weechat
> pip install websocket-client
> pip list
pip (9.0.3)
setuptools (39.0.1)
six (1.11.0)
websocket-client (0.47.0)
wheel (0.30.0)
> weechat
         โ”‚17:22:10     |   ___       __         ______________        _____ 
         โ”‚17:22:10     |   __ |     / /___________  ____/__  /_______ __  /_
         โ”‚17:22:10     |   __ | /| / /_  _ \  _ \  /    __  __ \  __ `/  __/
         โ”‚17:22:10     |   __ |/ |/ / /  __/  __/ /___  _  / / / /_/ // /_  
         โ”‚17:22:10     |   ____/|__/  \___/\___/\____/  /_/ /_/\__,_/ \__/  
         โ”‚17:22:10     | WeeChat 2.1 [compiled on Mar 25 2018 12:05:32]
         โ”‚17:22:10     | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
         โ”‚17:22:10     | python: stdout/stderr (?): Traceback (most recent call last):
         โ”‚17:22:10     | python: stdout/stderr (?):   File "/Users/neverfox/.weechat/python/autoload/wee_slack.py",
         โ”‚             | line 28, in <module>
         โ”‚17:22:10     | python: stdout/stderr (?):     from websocket import create_connection,
         โ”‚             | WebSocketConnectionClosedException
         โ”‚17:22:10     | python: stdout/stderr (?): ImportError: No module named websocket
         โ”‚17:22:10 =!= | python: unable to parse file "/Users/neverfox/.weechat/python/autoload/wee_slack.py"
         โ”‚17:22:10     | script: 347 scripts for WeeChat 2.1
         โ”‚17:22:10     | Plugins loaded: alias, aspell, buflist, charset, exec, fifo, fset, irc, logger, lua, perl,
         โ”‚             | python, relay, ruby, script, tcl, trigger, xfer

Either weechat isn't respecting the virtualenv or something else is wrong with the import of the websocket module. Thoughts?

Most helpful comment

As said, this has to do with weechat loading python plugins using libpython instead of calling the python executable. However, normal virtualenvs works fine because they seem to be loaded a bit differently than pyenv. As far as I understand, this is how they work:

Normal virtualenvs just change the PATH so the python executable in the virtualenv is available. Even when loaded with libpython, python uses the location of the executable to determine where to look for modules.

However, pyenv place a wrapper script on the PATH instead which sets up the correct path to the python modules when executed. When loading with libpython, python can't use this as the wrapper script has no relation to the python modules.

To make the virtualenv work, you can use the activate script it provides, instead of pyenv commands:

source ~/.pyenv/versions/weechat/bin/activate

I don't know if it's possible to make it work without this step, I would suggest asking in the pyenv-virtualenv repo for more info.

All 11 comments

Why would weechat respect pyenv or virtualenv? All weechat does is look for a libpython on the library path. Neither pyenv nor virtualenvs touch that.

@neverfox I had this same issue.

I ended up installing it globally for me under pip install --user.

However, have you tried updating the #! in the wee_slack.py file? Perhaps to
/usr/bin/env python?

Why would weechat respect pyenv or virtualenv? All weechat does is look for a libpython on the library path. Neither pyenv nor virtualenvs touch that.

Well, fair enough, but not knowing that or much about libpython, I would have asked the opposite: why wouldn't it? But the main reason I expected it to work has to do with comments like this (old as it is and perhaps no longer relevant) that apparently worked at the time and didn't elicit any response that it was out of the ordinary. I'm one of those people who is pretty loathe to use package managers for system installed language X because it starts to get out of hand over time. I was hoping that there was a way to use virtualenvs for this kinds of thing. I might try @jalcine's suggestion (update: which unfortunately doesn't work).

It's linked against a particular version of Python. If it's a static link, then you might be out of luck.

However, if it's not _statically_ linked, you might be able to do the virtualenv setup for weechat and get around this altogether.

I found this same issue in an anaconda environment. This is usually installed as a default python installation, so wee-slack shows a similar error:

         โ”‚17:49:11     | python: stdout/stderr (?):   File
         โ”‚             | "/home/user/anaconda2/envs/slack-env-2/lib/python2.7/site-packages/websocket/_core.py", line
         โ”‚             | 33, in <module>
         โ”‚17:49:11     | python: stdout/stderr (?):     from ._handshake import *
         โ”‚17:49:11     | python: stdout/stderr (?):   File
         โ”‚             | "/home/user/anaconda2/envs/slack-env-2/lib/python2.7/site-packages/websocket/_handshake.py",
         โ”‚             | line 30, in <module>
         โ”‚17:49:11     | python: stdout/stderr (?):     from ._http import *
         โ”‚17:49:11     | python: stdout/stderr (?):   File
         โ”‚             | "/home/user/anaconda2/envs/slack-env-2/lib/python2.7/site-packages/websocket/_http.py", line
         โ”‚             | 30, in <module>
         โ”‚17:49:11     | python: stdout/stderr (?):     from ._logging import *
         โ”‚17:49:11     | python: stdout/stderr (?):   File
         โ”‚             | "/home/user/anaconda2/envs/slack-env-2/lib/python2.7/site-packages/websocket/_logging.py", line
         โ”‚             | 22, in <module>
         โ”‚17:49:11     | python: stdout/stderr (?):     import logging
         โ”‚17:49:11     | python: stdout/stderr (?):   File
         โ”‚             | "/home/user/anaconda2/envs/slack-env-2/lib/python2.7/logging/__init__.py", line 26, in <module>
         โ”‚17:49:11     | python: stdout/stderr (?):     import sys, os, time, cStringIO, traceback, warnings, weakref,
         โ”‚             | collections
         โ”‚17:49:11     | python: stdout/stderr (?):   File
         โ”‚             | "/home/user/anaconda2/envs/slack-env-2/lib/python2.7/weakref.py", line 14, in <module>
         โ”‚17:49:11     | python: stdout/stderr (?):     from _weakref import (
         โ”‚17:49:11     | python: stdout/stderr (?): ImportError: cannot import name _remove_dead_weakref
         โ”‚17:49:11 =!= | python: unable to parse file "/home/user/.weechat/python/autoload/wee_slack.py"
         โ”‚17:49:11     | Plugins loaded: alias, aspell, buflist, charset, exec, fifo, fset, irc, logger, perl, python,
         โ”‚             | relay, ruby, script, trigger, xfer


What is the reason wee-slack is not working in virtual environments? It should respect the path according to /usr/bin/env python . The previous comment suggests that the solution depends on whether wee-slack is statically linked, so I would be interested in a more complete explanation.

Thanks!

It should respect the path according to /usr/bin/env python .

No, wee-slack is not a Python _program_. It is a script loaded into a Python interpreter embedded into weechat, typically using the system libpython.

Well, now are you're explaining why there are issues in some virtual environments.

Python interpreter embedded into weechat

So I assume this is not a problem specific to wee-slack, but any other plugin that could be written for weechat might ignore the desired python interpreter in $PATH. Is this correct?

As said, this has to do with weechat loading python plugins using libpython instead of calling the python executable. However, normal virtualenvs works fine because they seem to be loaded a bit differently than pyenv. As far as I understand, this is how they work:

Normal virtualenvs just change the PATH so the python executable in the virtualenv is available. Even when loaded with libpython, python uses the location of the executable to determine where to look for modules.

However, pyenv place a wrapper script on the PATH instead which sets up the correct path to the python modules when executed. When loading with libpython, python can't use this as the wrapper script has no relation to the python modules.

To make the virtualenv work, you can use the activate script it provides, instead of pyenv commands:

source ~/.pyenv/versions/weechat/bin/activate

I don't know if it's possible to make it work without this step, I would suggest asking in the pyenv-virtualenv repo for more info.

Arch users ( especially those using Luke Smith's LARBS ) will find the solution to this in issue #122 by @jferris
https://github.com/wee-slack/wee-slack/issues/122#issuecomment-157499916

I had the same issue on OpenSuse 42.3.
I installed pip2 explicitly and then I installed again the websocket-client.
pip2 install websocket-client

That solved for me.

Provably unrelated, or not. I had the same error. I fixed just running pip3

"pip3 install websocket_client"

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SevereOverfl0w picture SevereOverfl0w  ยท  3Comments

matthijskooijman picture matthijskooijman  ยท  6Comments

karlp picture karlp  ยท  6Comments

tuananh picture tuananh  ยท  9Comments

neverfox picture neverfox  ยท  4Comments