Using Python Virtual Environments with Terminator
Asked Answered
D

1

7

With Terminator, a user can define layouts within the configuration file. With these layouts, the user can set a command to be executed at start-up. So, for example, one could create a layout in which a terminal automatically executed ls like this (note the bash command to avoid the terminal closing):

command = "ls; bash"

Now, how can I make Terminator load a Python Virtual Environment instead? Keeping, of course, the bash console active with the environment loaded.

Note

The trivial way:

command = "workon my_env; bash"

or its source my_env/bin/activate equivalent (without using virtualenvwrapper), wont work.

Dockyard answered 20/7, 2015 at 19:13 Comment(0)
D
11

The trick is to do everything with just "one" command: bash. Taking advantage of its -i option (interactive) and using a custom --rcfile in which PROMPT_COMMAND is set to whatever we want to execute. The result would be like this:

command = "bash --rcfile <(cat ${HOME}/.bashrc; echo 'export PROMPT_COMMAND="workon my_env; unset PROMPT_COMMAND"') -i"

Explanation

  • We execute bash in interactive (-i) mode.
  • We execute commands from a custom command file (--rcfile) instead of .bashrc.
  • This file is created with the contents of .bashrc plus one more command.
  • This extra command exports PROMPT_COMMAND with a value of "whatever we want to execute". In this case: workon my_env.
  • The PROMPT_COMMAND is unset just after it is executed the first time to avoid multiple executions after each interaction with the shell.

One could easily extend the custom command just editing the part workon my_env. So, for example, if you want to automatically execute ls appart from loading the Virtual Environment, you would write workon my_env; ls instead.

Dockyard answered 20/7, 2015 at 19:33 Comment(2)
unfortunately that does not work for me, I get error /bin/bash: =: command not found /bin/bash: line 0: unset: ´PROMPT_COMMAND') -i': not a valid identifierAho
This does finally work for me as well. Any chance to actually also cancel that command? mine runs forever.Wellestablished

© 2022 - 2024 — McMap. All rights reserved.