BLOGROLLS

Tmux Python Virtual Environment Pane/Window Loading Issue

By: wormius On: Fri 20 December 2024
In: misc
Tags:

I was having an issue with tmux not properly loading Better Window Names for tmux.

Constantly was receiving an error stating :

tmux display "ERROR: tmux-window-name - Python dependency libtmux not found (Check the README)"

So I did a pip list and it showed libtmux installed. I also did python3 -m pip list, which also showed libtmux.

So - I figured if in doubt, try installing again. I tried to use pip to install libtmux (using "pip install --user libtmux" per the README.md), only to find out that's deprecated, and python3 -m pip... is the preferred method since 2021. How do I know this? The following error arises if you try to install. I did notice that the system had the libtmux package installed via apt; so I removed that in case there was a conflict, but still received the below message.

error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.

    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.

    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.

    See /usr/share/doc/python3.11/README.venv for more information.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

This was confusing me as I was showing libtmux installed. I went to inspect the tmux_window_name.tmux source.

pip_list=$(python3 -m pip list 2> /dev/null)
if ! echo "$pip_list" | grep libtmux -q; then
    tmux display "ERROR: tmux-window-name - Python dependency libtmux not found (Check the README)"
    exit 0
fi

I'm not super familiar with Bash script, but fucked around a bit with it.
My first intuition was there was an issue with the piping to /dev/null, that wasn't it. Removed that and then opened tmux and reloaded the plugins, but still failing with the libtmux error.

While in the process of trying to sort out the issue, I opened a new window and noticed it was opening a python virtual environment. Using the command echo $VIRTUAL_ENV it indicated one of my python projects.

I was having an issue with this earlier, and thought it was due to me having set the venv before opening tmux. In the process of fixing that, I was unable to "deactivate" the venv. I found out the solution there is to go into a venv (supposedly any venv), and activate it, and then it would allow me to deactivate like normal.

So I did that, and wasn't noticing any further issues (though I hadn't opened any panes or windows after that). However, now, as noted above, I noticed it was still happening after opening a new window to test the window renaming in general (with the plugin not loaded). There was the venv active in a new window (and further testing reveals new panes are doing this as well).

Once I noticed there was a window opening with a default environment. This wasn't happening on attaching a previous tmux session, so I wasn't noticing it. (I tested this by typing "clear" in case the venv loaded, but not updating the prompt, for example, perhaps the prompt was being rendered, and THEN the venv was loaded).

I did a pip list (python -m pip list, because I'm getting the hang of this a little, methinks). Indeed, I only had a couple of the packages installed for that environment/project (as I had expected). libtmux was NOT installed in this list (a good thing).

To test my hypothesis, I did a python3 -m - pip install libtmux in the environment that was auto-loading on a new pane or window, and sure as shit, when I reloaded the plugins I was no longereceiving the error message.

So as it stands, I know that the main issue is when i attach a tmux session, it seems to be putting it into the venv of the project. Now that I've added the libtmux to that project venv, it is loading properly.

SO the real solution here is to figure out WHY new panes and windows are putting me into a venv.

I have a feeling it has to do with one of my session saving plugins:

# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'ofirgall/tmux-window-name'
set -g @plugin 'jaclu/tmux-mouse-swipe'
set -g @plugin 'joshmedeski/tmux-nerd-font-window-name'
set -g @plugin 'erikw/tmux-powerline'
set-option -g @plugin 'b0o/tmux-autoreload'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
# Plugin options
set -g @resurrect-capture-pane-contents 'on'
set -g @continuum-restore 'on'
set -g @continuum-save-interval '5'

Now is it tmux-autoreload, tmux-continuum or tmux-resurrect? Time to sleuth some more.

I do wonder if there's some sort of "hung" session on one of these that hasn't properly deactivated, so it's constantly trying to load a new pane/window in that venv. I'm not sure the right term, exactly, but it seems that's the most likely culprit.

EDIT: So - one issue I noticed was that I had both a ~/.config/tmux/ folder and a ~/.tmux/ folder. I wonder if this was causing confusion, so I renamed the .tmux folder to see if that affected my tmux environment, and it did (by removing the status bar at the bottom). This indicated I was editing the wrong file (~/.config/tmux), when I thought was making proper changes.

So - I renamed the folder back to ~/.tmux, loaded tmux to ensure it was working again, and it did.

For shits and giggles, I tried to open a new pane, and LO! THE VENV WAS NO LONGER LOADING!
Likewise I tried to open a new window, and again - no VENV. WTH?
All I did was rename my .tmux config file and back, and suddenly... It was now working?

To reduce any further confusion about what config files I was editing, I deleted the .config/tmux file. Now, I only have one place for the settings.

I'm not sure if having .tmux in my home folder is the correct way or if I set it like that due to some tutorial. It seems to me having the tmux configs in the .config would be more technically correct. But I am not going to mess with that anymore.

Computers are fuckin' WEIRD man.

I'm assuming it had to do with some sort of cache issue, and when I renamed the file, and loaded tmux, it reverted to something and then cleared whatever was broken in a cache to default, and then the settings were restored. I'm not giving a shit to try any further, but uh.

If you have a python virtual environment loading when you open a new pane/window, try renaming your settings folder to a temp name and back to the original name and see if that fixes it.


If you found the article helpful, please share or cite the article, and spread the word:


<