简体   繁体   中英

Tmux split window and activate a python virtualenv

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.

First create all of your panes.

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

tmux send-keys -t development:0.1 "workon some_env" Cm

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.

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.

Here's how I do this:

Cb Cy pact

Explanation:

Cb is my tmux default prefix.

Cy 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM