简体   繁体   中英

Python virtual environment not activating from bash script

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
  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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM