0

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.

black_hole_sun
  • 908
  • 11
  • 41
  • 2
    Remove the `exit` from the script if you don't want it to make the invoking shell exit itself when `source`d. There's **never** a reason to make `exit` or `exit "$?"` the last line of a script -- that's what it _always_ does when it reaches the end no matter what! – Charles Duffy Jul 18 '20 at 21:40
  • 1
    This is arguably a duplicate of [exit from source'd script](https://stackoverflow.com/questions/49857332/bash-exit-from-source-d-script), or [terminal closes when I start script with dot at the start](https://stackoverflow.com/questions/53596107) (keeping in mind that `.` is a synonym for `source`). – Charles Duffy Jul 18 '20 at 21:43
  • 1
    Also remove the `#!` line from the top - there is no useful purpose in executing (as opposed to sourcing) such a script, because doing so cannot affect any variables in the parent shell, and any `#!` line is only relevant to executing a script. The purpose of sourcing the activate script is to set relevant environment variables. – alani Jul 18 '20 at 21:43

3 Answers3

0

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)

black_hole_sun
  • 908
  • 11
  • 41
0

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.

Robert
  • 7,394
  • 40
  • 45
  • 64
-1

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)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547