-
Notifications
You must be signed in to change notification settings - Fork 43
/
pythonmac
executable file
·193 lines (178 loc) · 7.67 KB
/
pythonmac
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash
# pythonmac: detects a python executable which has access to the screen on MacOS
#
# usage:
# ./pythonmac File.py [Arguments]
#
# background:
# On MacOS, wxPython cannot access the screen with a python version which is not a
# "framework" version.
#
# The following script: `import wx. wx.App();` will fail with the error:
# " This program needs access to the screen. Please run with a
# Framework build of python, and only when you are logged in
# on the main display of your Mac."
#
# The "Framework build of python" are in different locations:
# - for the system python:
# /Library/Frameworks/Python.framework/Versions/XXX/bin/pythonXXX
# or
# /System/Library/Frameworks/Python.framework/Versions/XXX/bin/pythonXXX
#
# - for python installed with `brew` (likely in `/usr/local`):
# $(brew --suffix)/Cellar/python/XXXXX/Frameworks/python.framework/Versions/XXXX/bin/pythonXXX");
# - for python installed with anaconda, typically:
# /anaconda3/bin/python.app (NOTE: the '.app'!")
#
# The following attempts to detect which version of python is used, whether the user
# is currently in a virtual environment or a conda environment, and tries to find the
# proper "python" to use that has access to the screen.
#
# created: January 2019
# author : E.Branlard
# website: https://github.com/ebranlard/pyDatView
#
DEBUG=1
# --- Detecting some user settings
if [ -x "$(command -v brew)" ]; then
HAS_BREW=1
else
HAS_BREW=0
fi
if [ -x "$(command -v python3)" ]; then
HAS_PY3=1
else
HAS_PY3=0
fi
if [ -z "$VIRTUAL_ENV" ] ; then
IS_VIRTUAL_ENV=0
else
IS_VIRTUAL_ENV=1
fi
CONDA_IN_PYTHON=`which python | grep conda |wc -l|xargs`
if [ -z "$CONDA_PROMPT_MODIFIER" ] ; then
CONDA_ACTIVE=0
else
CONDA_ACTIVE=1
fi
CURR_PYVER="$(python --version 2>&1 | cut -d ' ' -f2)" # e.g., 2.7.10
CURR_PYXpY="$(python --version 2>&1 | cut -d ' ' -f2|cut -c 1-3)" # e.g., 2.7 or 3.7
CURR_PYN="$(python --version 2>&1 | cut -d ' ' -f2|cut -c 1)" # e.g., 2 or 3
if [ "$DEBUG" == "1" ] ; then
echo "[INFO] HAS BREW : $HAS_BREW"
echo "[INFO] HAS PY3 : $HAS_PY3"
echo "[INFO] PYTHON_VER N : $CURR_PYN"
echo "[INFO] PYTHON_VER N.X : $CURR_PYXpY"
echo "[INFO] PYTHON_VER : $CURR_PYVER"
echo "[INFO] IS_VIRTUAL_ENV : $IS_VIRTUAL_ENV"
echo "[INFO] CONDA_IN_PYT : $CONDA_IN_PYTHON"
echo "[INFO] CONDA_ACTIVE : $CONDA_ACTIVE"
fi
# --- Finding Framework python
FRAMEWORK_PYTHON_ROOT="/Library/Frameworks/Python.framework/Versions"
#echo $FRAMEWORK_PYTHON_ROOT
FRAMEWORK_FOUND=1
if [ ! -d $FRAMEWORK_PYTHON_ROOT ]; then
#echo "Framework not found in /Library/. Trying in /System"
FRAMEWORK_PYTHON_ROOT="/System/Library/Frameworks/Python.framework/Versions"
#echo $FRAMEWORK_PYTHON_ROOT
if [ ! -d $FRAMEWORK_PYTHON_ROOT ]; then
FRAMEWORK_FOUND=0
echo "[WARN] Framework python directory not found, things may not work"
fi
fi
if [ "$DEBUG" == "1" ] ; then
echo "[INFO] FRAMEWORK_ROOT : $FRAMEWORK_PYTHON_ROOT"
fi
# --- Python exe - TODO, try python3 first
#if [ "$FRAMEWORK_FOUND" == "1" ]; then
# PYVER=2.7
# FRAMEWORK_PYTHON="$FRAMEWORK_PYTHON_ROOT/$PYVER/bin/python$PYVER"
#else
# # Try /usr/bin/python
# echo "[FAIL] Cannot find a system python installation"
# echo " Find the path to your system python."
# echo " Use the system python to launch pyDatView.py"
#fi
#if [ ! -f $FRAMEWORK_PYTHON ]; then
#fi
#PYTHON_EXE=$FRAMEWORK_PYTHON
# --- Setting up PYTHONHOME and PYTHON_EXE
if [ "$CONDA_ACTIVE" == "1" ] ; then
# TODO: NEW we use pythonw
echo "[INFO] It seems you are in a conda environment, trying with conda python.app"
#
#PYTHON_EXE=`which conda|rev|cut -c 6-|rev`"python.app"
PYTHON_EXE=pythonw
# NOTE: error message needs updating
HELP="[HELP] If a weird error appears(e.g. loading pandas), try 'conda update python.app'"
elif [ "$IS_VIRTUAL_ENV" == "0" ] ; then
if [ "$CONDA_ACTIVE" == "1" ] ; then
echo "[INFO] It seems you are in a conda environment, trying with conda python.app"
#
PYTHON_EXE=`which conda|rev|cut -c 6-|rev`"python.app"
HELP="[HELP] If a weird error appears(e.g. loading pandas), try 'conda update python.app'"
elif [ "$CONDA_IN_PYTHON" == "1" ] ; then
PYTHON_EXE=`which python`".app"
HELP="[HELP] If a weird error appears (e.g. loading pandas), try 'conda update python.app'"
else
echo "[WARN] You are not in a virtualenv. Things might not work."
if [ "$HAS_PY3" == "1" ] && [ "$HAS_BREW" == "1" ] ; then
echo "[INFO] Continuing assuming that you used pip3 to install the requirements and a brew python3"
PYTHON_EXE=python3
HELP="[HELP] If you have a module import error, use 'pip3 install --user -r requirements.txt'"
else
echo "[WARN] don't know which python to use, using default"
PYTHON_EXE=python
# find the root of the virtualenv, it should be the parent of the dir this script is in
#ENV_FROM_DIR=`dirname "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"`
#ENV_FROM_PY=`$FRAMEWORK_PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"`
#ENV_HOME=$HOME/Library/Python/2.7/lib/python/site-packages/
#ENV_HOME=$HOME/Library/Python/2.7/
#echo "ENV from python: $ENV_FROM_PY"
#echo "ENV from dir: $ENV_FROM_DIR"
#echo "ENV from home: $ENV_HOME"
#export PYTHONHOME=$ENV_HOME
fi
fi
else
# --------------------------------------------------------------------------------}
# --- VIRTUAL ENV
# --------------------------------------------------------------------------------{
echo "[INFO] VIRTUAL_ENV: $VIRTUAL_ENV"
if [ "$CURR_PYN" == "2" ] ; then
echo "[INFO] You are using a version 2 of python. Using framework python"
PYTHON_EXE="$FRAMEWORK_PYTHON_ROOT/$CURR_PYXpY/bin/python$CURR_PYXpY"
if [ ! -f $PYTHON_EXE ]; then
echo "[FAIL] Framework python exe not found : $PYTHON_EXE"
exit 1
fi
elif [ "$HAS_BREW" == "1" ] ; then
echo "[INFO] Continuing assuming that the virtual env has a brew python"
BREW_VER=`ls -1 $(brew --prefix)/Cellar/python/ |grep $CURR_PYVER | head -1 `
FRAMEWORK_PYTHON_ROOT="$(brew --prefix)/Cellar/python/$BREW_VER/Frameworks/Python.framework/Versions"
echo $FRAMEWORK_PYTHON_ROOT
if [ ! -d $FRAMEWORK_PYTHON_ROOT ]; then
echo "[FAIL] Brew framework python not found: $FRAMEWORK_PYTHON_ROOT"
exit 1
fi
PYTHON_EXE="$FRAMEWORK_PYTHON_ROOT/$CURR_PYXpY/bin/python$CURR_PYXpY"
else
echo "[FAIL] This script does not support your configuration. "
echo " Try running 'python pyDatView.py' and figure how to make it work"
echo " Contact the developer to account for your configuration."
exit 1
fi
#VENV_SITE_PACKAGES="$VIRTUAL_ENV/lib/python$PYVER/site-packages"
# Ensure wx.pth is set up in the virtualenv
#cp "/Library/Python/$PYVER/site-packages/wxredirect.pth" "$VENV_SITE_PACKAGES/wx.pth"
# Use the Framework Python to run the app
export PYTHONHOME=$VIRTUAL_ENV
echo "[INFO] PYTHONHOME: $PYTHONHOME"
fi
# --- Launching python
echo "[INFO] Using: $PYTHON_EXE" "$@"
echo ""
echo $HELP
echo ""
exec $PYTHON_EXE "$@"