From f79b4bd3247385c4b72467bf530af55f23aa6b7a Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 3 Sep 2019 13:31:55 +0200 Subject: [PATCH] main: Support running from virtualenv This fixes #105 --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ main.py | 11 +++++++++++ 2 files changed, 49 insertions(+) diff --git a/README.md b/README.md index 595c244..5744d38 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,44 @@ support is still experimental. /python version +## Using virtualenv +If you want to install dependencies inside a virtualenv, rather than +globally for your system or user, you can use a virtualenv. +Weechat-Matrix will automatically use any virtualenv it finds in a +directory called `venv` next to its main Python file (after resolving +symlinks). Typically, this means `~/.weechat/python/venv`. + +To create such a virtualenv, you can use something like below. This only +needs to happen once: + +``` +virtualenv ~/.weechat/python/venv +``` + +Then, activate the virtualenv: + +``` +. ~/.weechat/python/venv/bin/activate +``` + +This needs to be done whenever you want to install packages inside the +virtualenv (so before running the `pip install` command documented +above. + + +Once the virtualenv is prepared in the right location, Weechat-Matrix +will automatically activate it when the plugin is loaded. This should +not affect other plugins, which seem to have a separate Python +environment. + +Note that this only supports virtualenv tools that support the +[`activate_this.py` way of +activation](https://virtualenv.pypa.io/en/latest/userguide/#using-virtualenv-without-bin-python). +This includes the `virtualenv` command, but excludes pyvenv and the +Python3 `venv` module. In particular, this works if (for a typical +installation of `matrix.py`) the file +`~/.weechat/python/venv/bin/activate_this.py` exists. + ## Uploads Uploads are done using a helper script, the script found under diff --git a/main.py b/main.py index db71a70..162cfa3 100644 --- a/main.py +++ b/main.py @@ -18,6 +18,17 @@ from __future__ import unicode_literals import os + +# See if there is a `venv` directory next to our script, and use that if +# present. This first resolves symlinks, so this also works when we are +# loaded through a symlink (e.g. from autoload). +# See https://virtualenv.pypa.io/en/latest/userguide/#using-virtualenv-without-bin-python +# This does not support pyvenv or the python3 venv module, which do not +# create an activate_this.py: https://stackoverflow.com/questions/27462582 +activate_this = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'venv', 'bin', 'activate_this.py') +if os.path.exists(activate_this): + exec(open(activate_this).read(), {'__file__': activate_this}) + import socket import ssl import textwrap