This repository was archived by the owner on Apr 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenable_python.sh
More file actions
executable file
·80 lines (70 loc) · 3.2 KB
/
enable_python.sh
File metadata and controls
executable file
·80 lines (70 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env bash
# shellcheck disable=SC2317
[[ $_ != "$0" ]] || { echo "This file must be sourced to be useful."; exit 1; }
export PYTHONDONTWRITEBYTECODE="Don't clutter with __pycache__ directories"
# alternately: see/use https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPYCACHEPREFIX
_python_path_additions () {
case ${OSTYPE} in
(darwin*)
# I prefer manually maintained 'Official' Python over homebrew's dependency updated one
# NB: installation will prepend it's versioned directory in ~/.bash_profile
add_to_my_path /Library/Frameworks/Python.framework/Versions/Current/bin
# Add most recent Python framework's pip install --user directory
local python_version
python_version="$(find "${HOME}/Library/Python" -depth 0 -type d -exec ls -t1 {} + | head -1)"
if [[ -n "$python_version" ]]; then
local user_script_path
printf -v user_script_path "${HOME}/Library/Python/%s/bin" "$python_version"
add_to_my_path "$user_script_path"
fi
;;
(*)
if type python3 &>/dev/null; then
if ! type pip3 &>/dev/null; then
: printf "Warning: using %s, no pip3\n" "$(type -p python3)" 1>&2
# TODO: what is the right thing on Linux/DSM, where system python3 does not
# install pip or pip3 (but linuxbrew adds it's own pip3)? A good excuse to
# try uv https://docs.astral.sh/uv/
fi
else
: # No python3, enabling is gonna be rough
fi
;;
esac
}
_virtualenv_setup () { # unused now that I use python -m venv
case $(uname -s) in
(Linux*)
if [ -z "${WORKON_HOME}" ] && [ -e "${HOME}/Envs" ]; then
export WORKON_HOME=${HOME}/Envs
fi
;;
(Darwin*)
if [ -z "${WORKON_HOME}" ] && [ -e "${HOME}/Documents/Envs" ]; then
export WORKON_HOME=${HOME}/Documents/Envs
fi
;;
(MINGW*) # e.g., Git Bash
export WORKON_HOME=~/Envs # But do you want to hack Python in this?
;;
esac
# Get `command` to tell which virtualenvwrapper and python3
VIRTUALENVWRAPPER_PATH="$(command -v virtualenvwrapper_lazy.sh)" && export VIRTUALENVWRAPPER_PATH
VIRTUALENVWRAPPER_PYTHON="$(command -v python3)" && export VIRTUALENVWRAPPER_PYTHON
# virtualenvwrapper complaining? you probably forgot to
# syspip install virtualenvwrapper when brew upgraded python
if [ -e "${WORKON_HOME}" ] && [ -e "${VIRTUALENVWRAPPER_PATH}" ]; then
export PIP_REQUIRE_VIRTUALENV=true
function syspip {
local pip
# Which is the 'system' pip: prefer local (but not virtualenv ones)
PATH=/usr/local/bin:/usr/bin:${PATH} pip=$(command -v pip3)
PIP_REQUIRE_VIRTUALENV="" ${pip} "$@"
}
. "${VIRTUALENVWRAPPER_PATH}"
elif [ ! -e "${WORKON_HOME}" ]; then
printf "\nNo WORKON_HOME directory; retry when one has been created or export WORKON_HOME=/path/to/where\n"
fi
}
_python_path_additions
unset _python_path_additions _virtualenv_setup