skip to Main Content

System:

  • Operating System: CentOS Linux 7 (Core) / CentOS Linux release 7.7.1908 (Core)
  • Kernel: Linux 3.10.0-1062.4.1.el7.x86_64
  • Server from Provider – i am just a user

Hello this is my Path to my virtual env activate, which is currently running a django app.

/home/username/.local/env_myapp/bin/activate

if run the command

$ source /home/username/.local/env_myapp/bin/activate

The env$ (my_env) starts in the terminal.

I wanted to write a little script to automate the env starting without putting the whole path every time in the console.

so i looked into the activate file…

# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly

alright so u wrote this few lines in my script.

my_env

 #!/usr/bin/bash
source /home/username/.local/env_myapp/bin/activate
echo "check - activate env"
exit
  • if i run $ my_env it only echos "check – activate"
  • if i run $ source my_env it runs to echo "check – activate" and then my terminal in VScode vanishes.

Question


So how to activate python env via linux script correctly?


Dear Stackoverflow, this Question was about how to start the env via script and not about exit – as you flag this as a duplicate. Furthermore ny question is solved with the comments under my question, but i can not "solve" it. So wrote an answer down below – see "update" and credits to answers.

3

Answers


  1. Chosen as BEST ANSWER

    Update

    thanks to @Charles Duffy @alaniwi

    source /home/username/.local/env_myapp/bin/activate
    echo "check - activate env"
    

    now the Script starts my env in the terminal.

    (did know how to close the Question, via Question comments. so i wrote the answers of @Charles Duffy @alaniwi here)


  2. So how to activate python env via linux script correctly?

    Read Advanced Linux Programming, syscalls(2) and execve(2) (almost every Linux program except /sbin/init is started with execve).

    So you might start your python script with a shebang, so #!/usr/bin/python (and make it executable with chmod(1)).

    Be aware of your $PATH variable (see environ(7) and exec(3)).

    Your Unix shell is probably GNU bash (see also getpwent(3) and passwd(5)), so read its documentation (I personally prefer zsh) or at least bash(1)

    Login or Signup to reply.
  3. You can add a shell function that automatically activates the environment when you cd into the directory. (This assumes that your pyenv is in the Python project directory, not e.g., in your $HOME/pyenv or such.)

    # Automatically en/dis-able Python virtual environments:
    function cd() {
        builtin cd "$@"
        set_python_env
    }
    
    function set_python_env() {
        env=".pyenv"
        if [[ -z "$VIRTUAL_ENV" ]]
        then
            # If env folder is found then activate the virtual env. This only
            # works when cd'ing into the top level Python project directory, not
            # straight into a sub directory. This is probably the common use
            # case as Python depends on the directory hierarchy.
            if [[ -d "$env" ]]
            then
                VIRTUAL_ENV_DISABLE_PROMPT=1
                source "$env/bin/activate"
            fi
        else
            # Check the current folder belong to the current VIRTUAL_ENV. If
            # yes then do nothing, else deactivate, and possibly re-activate if
            # we switched to another venv.
            parentdir="$(dirname "$VIRTUAL_ENV")"
            if [[ "$PWD"/ != "$parentdir"/* ]]
            then
                deactivate
                set_python_env
            fi
        fi
    }
    
    # Activate an environment immediately; that way it's activated when you
    # open the Terminal and it starts you in the Python project directory.
    set_python_env
    

    Either copy the above into your ~/.bashrc or source it from there.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search