Tmux split window and activate a python virtualenv

Question:

My favorite python developement environment is:

  • One large left pan for vim
  • Two small pans on the right for interactive consoles

Each pan shoud run a python virtualenv (using virtualenvwrapper). So here is the list of commands I have to type to setup my environment:

➜  ~ workon some_env
➜  ~ tmux splitw -h -l90
➜  ~ workon some_env
➜  ~ tmux splitw -v -p50
➜  ~ workon some_env

Putting these commands in a script and sourcing the file would unfortunately run workon some_env three times on the same pan rather than once on each pan.

Pretty simple, but pretty boring to type each time I want to setup a working environment.

I bound a shortcut to split the window correcty:

bind a source-file ~/.config/tmux/dev-session

This file dev-session contains:

splitw -h -l90
splitw -v -p50

However, I’m unable to automatically run workon some_env on each pan. I turned the problem in every way, I just can’t get it work.

splitw "workon some_env"
# Exits immediately as `workon` function terminates

splitw "echo 'workon some_env' | source /dev/stdin"
# Exits immediately as `source` terminates

splitw "workon_args=some_env zsh -f .some_custom_zshrc"
# Where .some_custom_zshrc contains:
#    workon $workon_args
# Does not work since running `workon some_env` from a script
# has no effect on the script caller

splitw "zsh -c 'workon some_env' -i"
# `workon some_env` seems to be executed in a separate environment
# and the virtualenv is no more activated within the interactive shell

I tried even more exotic things, but here I’m stuck.

Asked By: Antoine Pinsard

||

Answers:

First create all of your panes.

Use send-keys to write your commands into the specified pane and execute them using C-m. For example:

tmux send-keys -t development:0.1 "workon some_env" C-m

If you have three panes, then the second and third would be SESSION_NAME:0.1 and SESSION_NAME:0.2

Here is an example of my configuration:

tmux new-session -s development -n editor -d

tmux split-window -h -t development

tmux select-pane -t development:0.1

tmux split-window -v -t development

tmux send-keys -t development:0.0 "emacs -nw" C-m

tmux send-keys -t development:0.0 f8

tmux send-keys -t development:0.1 "ptpython" C-m

This book has a great chapter on setting up environments using Tmux.

Answered By: mitghi

what i do is that
i edit the activate script and add

tmux set-environment VIRTUAL_ENV $VIRTUAL_ENV

after that, inside activate script there is deactivate function, i edit that function and add

tmux set-environment -r VIRTUAL_ENV

this way after i start venv all windows and panes will open with venv inside that session. If i deactivate new windows will open normal.

Answered By: Eray Xx

Here’s how I do this:

C-b C-y pact

Explanation:

C-b is my tmux default prefix.

C-y is a key-binding for synchronize-panes toggle. This is what I have in my .tmux.config file:

# synchronize panes toggle
unbind C-s
bind C-y set-window-option synchronize-panes 

And finally, pact is an alias that simply activates the python environment that’s present in that directory. Here’s what I have in my .bash_alias file:

alias pact='. .venv/bin/activate'

Note that this assumes that the Python virtual environment directory is called .venv. I have another command that I use to create Python virtual env:

penv() { python -m venv .venv --prompt "$@" && . .venv/bin/activate; }

For example, if I am in a project directory called proj then I run penv proj to create a virtual environment for this project.

Answered By: Vishal
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.