Skip to content

Commit

Permalink
Windows anaconda3 2020 without path. Figure out the python path. (#824)
Browse files Browse the repository at this point in the history
* Windows anaconda3 2020 without path. Figure out the python path.

* Anaconda 2020 may need augmented path for numpy.

* On some windows must deal with non-working python.exe

* nrnpyenv.sh generates error if it cannot remove item from PATH.
  • Loading branch information
nrnhines committed Nov 15, 2020
1 parent 20c2d56 commit e68a690
Showing 1 changed file with 68 additions and 16 deletions.
84 changes: 68 additions & 16 deletions bin/nrnpyenv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function trypy {
a=`ls "$1" |grep "$2"`
if test "$a" != "" ; then
b=`cygpath -U "$1/$a/$3"`
c=`nrnbinstr "$4" "$b"`
c=`nrnbinstr "$4" "$b" 2> /dev/null`
if test "$c" != "" ; then
c=`cygpath -U "$c"`
c=`dirname "$c"`
Expand All @@ -75,20 +75,71 @@ function trypy {
fi
}

PYTHON=""
if test "$1" != "" ; then
if $WHICH "$1" >& /dev/null ; then
PYTHON="$1"
# On some windows systems python is an empty executable which, when
# launched in a Command Prompt, directs the user to the Microsoft Store.
# With bash, it returns a 128 exit status. So we loop until we
# find a working python (or no python). Each time a python is non-working
# we remove that path from the PATH. If not Windows, break out after first
# attempt at finding a Python.
while true ; do
PYTHON=""
# Priority is the argument, python3, python
if test "$1" != "" ; then
if $WHICH "$1" >& /dev/null ; then
PYTHON="$1"
fi
elif $WHICH python3 >& /dev/null ; then
PYTHON=python3
elif $WHICH python >& /dev/null ; then
PYTHON=python
fi

# do not do the following craziness if not Windows.
if test "$OS" != "Windows_NT" ; then
break
fi

if test "$PYTHON" == "" ; then
break
else
if $PYTHON -c 'quit()' >& /dev/null ; then #working
break
else # remove from PATH
oldpath="$PATH"
a="`$WHICH $PYTHON`"
b="`dirname \"$a\"`"
PATH="`echo \"$PATH\" | sed \"s,:$b:,:,\"`" #remove b from path if internal
PATH="`echo \"$PATH\" | sed \"s,^$b:,,\"`" #remove b from path if begin
PATH="`echo \"$PATH\" | sed \"s,:$b\$,\",`" #remove b from path if end
export PATH
if test "$oldpath" = "$PATH" ; then
echo "\"$b\", that contained a failing Python, did not get removed from PATH=\"$PATH\"" 1>&2
exit 1
fi
fi
fi
elif $WHICH python3 >& /dev/null ; then
PYTHON=python3
elif $WHICH python >& /dev/null ; then
PYTHON=python
else
done

if test "$PYTHON" = "" ; then
# Often people install Anaconda on Windows without adding it to PATH
if test "$OS" = "Windows_NT" -a "$APPDATA" != "" ; then
smenu="$APPDATA/Microsoft/Windows/Start Menu/Programs"
trypy "$smenu" Anaconda3 "Anaconda Prompt.lnk" activate.bat
if test "$PYTHON" = "" ; then
trypy "$smenu" "Anaconda3 (64-bit)" "Anaconda Prompt (anaconda3).lnk" activate.bat
# Anaconda3 2020 may need more PATH for numpy to work.
if test "$PYTHON" != "" ; then
if ! $PYTHON -c 'import numpy' >& /dev/null ; then
# first item added in trypy
a="`echo $PATH | sed 's/:.*//'`"
export PATH="$PATH:$a/Library/mingw-w64/bin:$a/Library/usr/bin:$a/Library/bin:$a/Scripts:$a/bin:$a/condabin"
# Actually get this PATH when scripts do a -- eval "`nrnpyenv.sh`"
echo "export PATH=\"$PATH\""
fi
fi
fi
if test "$PYTHON" = "" ; then
trypy "$smenu" Anaconda3 "Anaconda Prompt.lnk" activate.bat
fi
if test "$PYTHON" = "" ; then
trypy "$smenu" Anaconda2 "Anaconda Prompt.lnk" activate.bat
fi
Expand All @@ -103,10 +154,11 @@ else
fi
fi
fi
if test "$PYTHON" = "" ; then
echo "Cannot find executable python3 or python" 1>&2
exit 1;
fi
fi

if test "$PYTHON" = "" ; then
echo "Cannot find executable python3 or python" 1>&2
exit 1;
fi

echo "# PYTHON=`$WHICH $PYTHON`"
Expand Down Expand Up @@ -433,7 +485,7 @@ foo = [i for i in foo if s not in i]
print ("# in neither location " + str(foo))
print ("# " + spname + " = " + sp)
print ("# site-3 = " + s)
if "darwin" in sys.platform or "linux" in sys.platform or "win" in sys.platform:
# What, if anything, did python prepend to PATH
path=""
Expand Down

0 comments on commit e68a690

Please sign in to comment.