1

Now before you mark this as a duplicate, I have tried the solution posted here and they aren't working for me. I tried making an alias and I tried creating a function as so:

activate () {
    echo Activating Virtual Environment...
    source alexa/bin/activate
}
activate

But my script just gets run through without a virtual environment getting activated. The script is being run from the same directory as my virtual environment directory, alexa.

For clarity, the other solution I tried was to make an alias:

alias activate="source alexa/bin/activate"
activate

That didn't work and gave me an error that ./alexaEnvSetup.sh: line 43: activate: command not found.

Any thoughts or ideas?

EDIT: I think it is worth mentioning that the echo command does print out when I do this. So the function is getting entered. The virtual environment is just not getting activated.

EDIT: Adding full code:

#!/bin/bash

if [[ "$OSTYPE" == "linux-gnu" ]]; then
    echo Operating system: Linux
elif [[ "$OSTYPE" == "darwin"* ]]; then
    echo Operating system: Mac OSX
    echo

    # Install Python 3.6.5 using `curl`
    curl -O https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
    tar xf Python-3.6.5.tgz
    cd Python-3.6.5
    ./configure
    make
    make install

    echo
    echo Python Version 3.6.5 Installed
    echo

    # Install Pip
    curl -O http://bootstrap.pypa.io/get-pip.py
    /usr/local/bin/python3.6 get-pip.py

    echo
    echo Pip Installed
    echo

    # Install virtualenv
    pip install virtualenv

    echo
    echo Virtual Environment Installed

    virtualenv -p python3 alexa
    echo Created Virtual Environment, \"alexa\"

    activate () {
        echo Activating Virtual Environment...
        source /Users/XXXX/Auto-Lab/Commerce/alexa/bin/activate
    }
    export -f activate
    activate

    echo Virtual Environment, \"alexa\", Created and Activated
    echo

    # All packages (time, urllib, and json) should come default with Python3

elif [[ "$OSTYPE" == "cygwin" ]]; then
    # POSIX compatibility layer and Linux environment emulation for Windows
    echo Operating system: Cygwin
elif [[ "$OSTYPE" == "msys" ]]; then
    # Lightweight shell and GNU utilities compiled for Windows (part of MinGW)
    echo Operating system: Msys
elif [[ "$OSTYPE" == "win32" ]]; then
    echo Operating system: Windows32
elif [[ "$OSTYPE" == "freebsd"* ]]; then
    echo Operating system: FreeBSD
else
    echo Operating system unknown.
fi
peachykeen
  • 4,143
  • 4
  • 30
  • 49
  • 2
    Aliases are usually not available in scripts, check the spelling and scoping and so forth to make sure `activate` is in fact available where you try to use it. Without seeing that script it's hard to help too much (your function example works fine for me). You might want to check your script in shellcheck.net and see if that finds any problems for you – Eric Renouf Jun 18 '18 at 16:27
  • What if you just remove the alias function? Just call ``` echo Activating Virtual Environment... source /Users/XXXX/Auto-Lab/Commerce/alexa/bin/activate```? Often times a script runs, activates the env, then exits the env. Have you verified it is not working without your activate function? – Scott Skiles Jun 18 '18 at 17:51
  • It "works" the same with and without the activate function. I noticed no difference in behavior. So I got it to work without any functions by following https://askubuntu.com/questions/965475/cannot-activate-virtual-environment-with-a-shell-script . But, this requires me to launch my script using `source alexaEnvSetup.sh`. Is this appropriate to do? It would be nice to just have it be `./xxxx` to run it. – peachykeen Jun 18 '18 at 17:55

2 Answers2

0
  1. use the full path to the activate script in the function: source /path/to/activate
  2. export the function: export -f activate
  3. ensure the script is a bash script: #!/bin/bash
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Hmmm, I tried this and it still isn't working. So I made my path: `source /Users/XXXXX/Auto\ Lab/Commerce/alexa/bin/activate` and then I added `export -f activate` on the line after the closing bracket of my function. Finally I called the function on the next line via `activate`. – peachykeen Jun 18 '18 at 16:46
0

The following works for me using Anaconda virtual env, perhaps it will work with yours also?

#!/usr/bin/env bash

# do bash stuff

# Python env
PATH=/home/username/path/to/activate/bin
python -u /script/to/run
Scott Skiles
  • 3,647
  • 6
  • 40
  • 64
  • Is it 'virtualenv' you are using? – Scott Skiles Jun 18 '18 at 17:21
  • Yes it is. So right before trying to activate my virtual environment, I create it using the following command: `virtualenv -p python3 alexa` – peachykeen Jun 18 '18 at 17:22
  • Does the creation right before activation cause an issue? Are you doing this anywhere else? I've only ever activated virtual envs from bash which have already existed. – Scott Skiles Jun 18 '18 at 17:35
  • 1
    There are no errors currently. The virtual env is now made. I can activate it just fine from terminal. But when trying from the script it fails. I will post my whole code above so you can see. – peachykeen Jun 18 '18 at 17:37