zellij icon indicating copy to clipboard operation
zellij copied to clipboard

How to activate a python environment on load of a layout file

Open satyamjay-iitd opened this issue 1 year ago • 3 comments

I want to do something very simple(or so I thought). Create two tabs, one with helix opened, and other with just a shell.

I wrote the following layout file.

layout {
    tab name="editor" {
       pane command="zsh" {
            args "-c" "source venv/bin/activate && hx"
            cwd "~/dev/calcite-qp/python"
        }
    }

    tab name="shell" {
        pane command="zsh" {
            args "-c" "source venv/bin/activate"
            cwd "~/dev/calcite-qp/python"
        }
    }
}

The editor opens fine, but the shell is not working. Kindly point in the right direction.

satyamjay-iitd avatar May 20 '24 10:05 satyamjay-iitd

Not a direct answer, but maybe look into the direnv tool? It will automatically load a python env whenever you cd into or otherwise load a directory in your shell. Super super handy. I have it load python and some environment variables some tools need. It is also a generic tool that works across shells and other programming languages. It can load all kinds of things.

https://direnv.net/

shaleh avatar May 24 '24 20:05 shaleh

Encountered the same problem. While not against using direnv, I am intending to use Zellij for collaborative work, so don`t want to have extra dependencies. Maybe there is some workaround to make Zellij source venv/bin/activate on startup or something like that?

susludusrotalis avatar Jun 09 '24 09:06 susludusrotalis

@satyamjay-iitd Think I`ve found the solution. You can source virtualenv like this:

layout {
    pane command="bash" {
        args "-c" "bash --rcfile <(cat ~/.bashrc; echo 'source venv/bin/activate';)"
    }
}

I'm still not sure exactly as to why it works as though starting bash with bash -c should`ve resulted in both bash shells to be closed. But it works. <(command_list)> is the Bash Process Substitution syntax

susludusrotalis avatar Jun 10 '24 14:06 susludusrotalis

I wrote a simple bash script to activate my venv, run the command, etc and just updated the pane command to this:

layout {
    pane command="zsh" {
        args "backend_dev.sh"
    }
}

BelardoA avatar Jul 16 '25 00:07 BelardoA