Python virtual environment not activating from bash script - python-3.x

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

use the full path to the activate script in the function: source /path/to/activate
export the function: export -f activate
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

Related

Anaconda M1 Mac Install Issues

I installed Python through Anaconda's 64-bit Graphical Installer Python 3.8 but when I checked the version it shows 2.7, any advice?
The below commands also does not work when I try to uninstall/remove anaconda:
rm -rf ~/anaconda3
conda install anaconda-clean ーー> (Error msg : zsh: command not found: conda)
anaconda-clean
anaconda-clean --yes
I tried using the command line installer and it doesn't work either.
Is there any way to safely remove Anaconda and the Anaconda Navigator?
Try typing python3 instead of just python.
Make sure that you're shell (bash / zsh) is initializing anaconda.
If you're using zsh, run cat ~/.zshrc and check for a script that looks like this:
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/<username>/anaconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/Users/<username>/anaconda3/etc/profile.d/conda.sh" ]; then
. "/Users/<username>/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/Users/<username>/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
If you're using bash, run cat ~/.bash_profile, but the script should be the same.
If it's not there, you may have missed something during installation. I would recommend just downloading the shell script (cli) installer and running again.
Ultimately, if that conda initialization script has executed successfully, you should see (base) before in your terminal.

Issue with Anaconda installation

I had previously installed ananconda and it was working fine but suddenly the Ubuntu gives busy box error so I had to run manual fsck command after starting the Anaconda stopped working.
ananconda -command not found.
I checked the bashrc file the PATH exported is anaconda3/bin but when I run echo $PATH it gives anaconda3/condabin. How do I resolve it.
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/home/anaconda3/etc/profile.d/conda.sh" ]; then
. "/home/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/home/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
You also have to remove the anaconda3/condabin from the $PATH variable.
export PATH=${PATH%:/home/YOUR_USER/anaconda3/condabin}

How to install gprbuild on Linux - Centos7

I have recently dowloaded GNAT Community on my Linux machine (Centos7).
Within /home/parallels/opt/GNAT/2019 there is a folder gprbuild, my understanding is that to install this I need to execute the bootstrap.sh script that is located within gprbuild:
/home/parallels/opt/GNAT/2019/gprbuild/bootstrap.sh
I try to execute the bootstrap.sh script like so...
[parallels#localhost gprbuild]$ ./bootstrap.sh
Then I recieve this error message...
./bootstrap.sh: line 87: gnatmake: command not found
Here is the bootstrap.sh script...
# bootstrap.sh - a simple bootstrap for building gprbuild with xmlada
progname=bootstrap
prefix=/usr/local
bindir=/bin
datarootdir=/share
libexecdir=/libexec
srcdir=$PWD
xmlada_src=../xmlada
CC=${CC:-cc}
GNATMAKE=${GNATMAKE:-gnatmake}
CFLAGS=${CFLAGS:-$CFLAGS}
GNATMAKEFLAGS=${GNATMAKEFLAGS:--j0}
usage() {
cat >&2 <<EOF
usage: $progname [options]
Options [defaults in brackets]:
--prefix=DIR installation prefix [$prefix]
--bindir=DIR user executables [PREFIX/bin]
--libexecdir=DIR program executables [PREFIX/libexec]
--datarootdir=DIR read-only arch.-independent data root [PREFIX/share]
--srcdir=DIR source code path [$PWD]
--with-xmlada=DIR xmlada source path [$xmlada_src]
--build build only but do not install
--install install only, skip build steps
Environment variables:
CC specify C compiler [$CC]
CFLAGS set C and Ada compilation flags [$CFLAGS]
DESTDIR optional for staged installs
GNATMAKE specify gnatmake Ada builder [$GNATMAKE]
GNATMAKEFLAGS additional Ada builder flags [$GNATMAKEFLAGS]
EOF
exit 0
}
error() {
printf -- "%s: $1" "$progname" "${#:2}" >&2
exit 1
}
while :; do
case $1 in
--prefix=?*) prefix=${1#*=} ;;
--bindir=?*) bindir=${1#*=} ;;
--libexecdir=?*) libexecdir=${1#*=} ;;
--datarootdir=?*) datarootdir=${1#*=} ;;
--srcdir=?*) srcdir=${1#*=} ;;
--with-xmlada=?*) xmlada_src=${1#*=} ;;
--build) MODE="build";;
--install) MODE="install";;
-h|-\?|--help) usage ;;
*=*) error '%s: Requires a value, try --help\n' "$1" ;;
-?*) error '%s: Unknown option, try --help\n' "$1" ;;
*) break # End of arguments.
esac
shift
done
set -e
inc_flags="-I$srcdir/src -I$srcdir/gpr/src -I$xmlada_src/sax -I$xmlada_src/dom \
-I$xmlada_src/schema -I$xmlada_src/unicode -I$xmlada_src/input_sources"
# Programs to build and install
bin_progs="gprbuild gprconfig gprclean gprinstall gprname gprls"
lib_progs="gprlib gprbind"
# Build
if [ "x"${MODE} == "x" ] || [ ${MODE} == "build" ];
then
command $CC -c $CFLAGS "$srcdir"/gpr/src/gpr_imports.c
for bin in $bin_progs; do
command $GNATMAKE $inc_flags "$bin"-main -o "$bin" $CFLAGS $GNATMAKEFLAGS -largs gpr_imports.o
done
for lib in $lib_progs; do
command $GNATMAKE $inc_flags "$lib" $CFLAGS $GNATMAKEFLAGS -largs gpr_imports.o
done
fi;
# Install
if [ "x"${MODE} == "x" ] || [ ${MODE} == "install" ];
then
mkdir -p "$DESTDIR$prefix$bindir"
mkdir -p "$DESTDIR$prefix$libexecdir"/gprbuild
mkdir -p "$DESTDIR$prefix$datarootdir"/gprconfig
mkdir -p "$DESTDIR$prefix$datarootdir"/gpr
install -m0755 $bin_progs -t "$DESTDIR$prefix$bindir"
install -m0755 $lib_progs -t "$DESTDIR$prefix$libexecdir"/gprbuild
install -m0644 "$srcdir"/share/gprconfig/*.xml -t "$DESTDIR$prefix$datarootdir"/gprconfig
install -m0644 "$srcdir"/share/gprconfig/*.ent -t "$DESTDIR$prefix$datarootdir"/gprconfig
install -m0644 "$srcdir"/share/_default.gpr "$DESTDIR$prefix$datarootdir"/gpr/_default.gpr
fi
I have been told that I need to install xmlada prior to installing gprbuild, then I have read elsewhere that I need to install gprbuild to be able to install xmlada!
I have a similair issue when attempting to install xmlada, the shell script within the xmlada folder is called install-sh, when I attempt to install this I am told there is no input file specified...
[parallels#localhost xmlada]$ ./install-sh
./install-sh: no input file specified.
I apreciate this is really two questions in one, but I felt I had to explain it this way, as I am unsure which library needs to be installed first, and also how do I actually install them.
Any help would be greatly apreciated! I hope you're all having a good weekend... :)
Thanks,
Lloyd
Just install gcc-ada, or search in your package manager for gcc-ada (may change its name), gnat* commands come in this package

Installing and running .sh file in linux mint

I'm using Linux mint and I want to install COMSOL. The problem is that, when I run below code in terminal, there is an error which doesn't make sense. Could anyone solve the problem?
The code I wrote:
./COMSOL_Server_Workaround.sh
the error is:
COMSOL Server Installer Work-around Script
2016 TeAM SolidSQUAD-SSQ
./COMSOL_Server_Workaround.sh: 14: [: Linux: unexpected operator
./COMSOL_Server_Workaround.sh: 15: [: Linux: unexpected operator
-e ERROR: This script should be executed
from the COMSOL Server installation directory
(where comsolsetup.log resides)
picture of files
COMSOL_Server_Workaround.sh contains:
#!/bin/sh
echo
echo COMSOL Server Installer Work-around Script
echo 2016 TeAM SolidSQUAD-SSQ
echo
# assume working dir is COMSOL installation root
COMSOLROOT="$PWD"
# check architecture
[ "`uname`" == "Linux" ] && COMSOL_TARS="fl fl_setup_glnxa64 fl_setup_maclnx fl_jvm_glnxa64 fl_glnxa64 mph_maclnx mph_glnxa64"
[ "`uname`" == "Darwin" ] && COMSOL_TARS="fl fl_setup_maclnx fl_maclnx fl_jvm_maci64 fl_maci64 mph_maclnx mph_maci64"
# tell user he has to run the script from COMSOL Server folder
[ ! -e "$COMSOLROOT/comsolsetup.log" ] && echo -e "ERROR: This script should be executed\n from the COMSOL Server installation directory\n (where comsolsetup.log resides)" && exit 1
# check if we can write to folder
[ ! -w "$COMSOLROOT/comsolsetup.log" ] && echo -e "ERROR: Cannot write to folder \"$COMSOLROOT\".\n If COMSOL Server was installed as root, run this script as root too\n otherwise run it as user which made the installation of COMSOL Server!" && exit 2
# let us extract the source location and check if it accessible
COMSOLDVD="`grep "^cs.installroot" "$COMSOLROOT/comsolsetup.log" | awk '{print $3}'`"
echo "COMSOL SERVER Installation Path = $COMSOLROOT"
echo "COMSOL Server Installation Source Path = $COMSOLDVD"
# check if source is accessible
[ ! -r "$COMSOLDVD/setupconfig.ini" ] && echo "The installaton source \"$COMSOLDVD\" id not accessible! Make sure the DVD is mounted" && exit 3
# now start untaring FL archives
for COMSOL_TAR in $COMSOL_TARS
do
tar -xf "$COMSOLDVD/archives/$COMSOL_TAR.isa" -C "$COMSOLROOT"
done
# Null the bin\tomcat\conf\login.properties
echo > "$COMSOLROOT/bin/tomcat/conf/login.properties"
# Mission complete
echo
echo All done! Enjoy!
echo
exit 0

Shell script to verify docker installation on Linux

I want to write a shell script which verifies whether docker is installed or not.
If docker is installed:
$ docker -v
Docker version 1.7.0, build 0baf609
$ echo $?
0
If docker is not installed:
$ docker -v
The program 'docker' is currently not installed. You can install it by typing:
apt-get install docker
$ echo $?
127
Here is my script:
#!/bin/bash
docker -v
if echo $? = 128 ; then
echo "The program 'docker' is currently not installed."
else
echo "Continuing with dockerized way"
fi
here for testing purpose, I ran it on the machine where docker is not installed, I kept 127 = 128, condition is wrong, so it should go in else, but still it prints The program 'docker' is currently not installed. I would like to know what I am missing here.
The correct syntax is:
if [ $? -eq 128 ]; then
...
To make it even more robust, you might want to check:
if [ $? -ne 0 ]; then
...

Resources