-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-venv-setup.sh
More file actions
177 lines (160 loc) · 5.27 KB
/
Copy pathpython-venv-setup.sh
File metadata and controls
177 lines (160 loc) · 5.27 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
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
#!/usr/bin/env bash
# Copyright (C) 2017 Martins Bruvelis <martins.bruvelis@gmail.com>
# Distributed under the GNU General Public License, version 3.0.
#
# Name: Python venv setup
# Version: 1.0.0
# Description: Bootstrapping script that enables users to download and install
# latest Python 3.6 using Miniconda and setup venv with Python libraries
# jupyter, pandas, keras, sklearn, seaborn tensorflow/tensorflow-gpu, etc.
# setup script configuration values
THIS_DIR=$(cd $(dirname $0); pwd)
THIS_FILE=$(basename $0)
THIS_PATH="$THIS_DIR/$THIS_FILE"
BATCH=0
TF_GPU=""
TF="tensorflow$TF_GPU"
MC_LATEST="Miniconda3-latest-Linux-x86_64.sh"
MC_URL="https://repo.continuum.io/miniconda/$MC_LATEST"
MC_PREFIX="$HOME/miniconda3"
MC_REUSE=0
VENV_PREFIX="$HOME/venv/$TF"
PYTHON_LIBRARIES="jupyter pandas keras sklearn h5py Pillow seaborn plotly bokeh $TF"
while getopts "bghm:rv:p:" x; do
case "$x" in
h)
echo "usage: $0 [options]
Installs latest Python using Miniconda3 and setup venv for TensorFlow.
-b run install in batch mode (without manual intervention),
it is expected the Miniconda3 license terms are agreed upon
-g install tensorflow-gpu, defaults to tensorflow
-h print this help message and exit
-m MC_PREFIX miniconda install prefix, defaults to $MC_PREFIX
-r reuse existing miniconda install prefix
-v VENV_PREFIX venv install prefix, defaults to $VENV_PREFIX
-p 'p1 p2' install python library, defaults to $PYTHON_LIBRARIES
"
exit 2
;;
b)
BATCH=1
;;
g)
TF_GPU="-gpu"
TF="tensorflow$TF_GPU"
VENV_PREFIX="$HOME/venv/$TF"
PYTHON_LIBRARIES="jupyter pandas keras sklearn h5py Pillow seaborn plotly bokeh $TF"
;;
m)
MC_PREFIX="$OPTARG"
;;
r)
MC_REUSE=1
;;
v)
VENV_PREFIX="$OPTARG"
;;
p)
PYTHON_LIBRARIES="$OPTARG"
;;
?)
echo "Error: did not recognize option, please try -h"
exit 1
;;
esac
done
# run script in interactive mode
if [[ $BATCH == 0 ]]
then
echo -n "
Welcome to Python vevn setup (by Martins Bruvelis)
Please, press ENTER to continue
>>> "
read dummy
echo -n "
Miniconda3 will be installed into this location:
$MC_PREFIX
- Press ENTER to confirm the location
- Press CTRL-C to abort the installation
- Or specify a different location below
[$MC_PREFIX] >>> "
read user_miniconda_prefix
if [[ $user_miniconda_prefix != "" ]]; then
case "$user_miniconda_prefix" in
*\ * )
echo "ERROR: Cannot install into directories with spaces" >&2
exit 1
;;
*)
eval MC_PREFIX="$user_miniconda_prefix"
;;
esac
fi
echo -n "
Python venv will be installed into this location:
$VENV_PREFIX
- Press ENTER to confirm the location
- Press CTRL-C to abort the installation
- Or specify a different location below
[$VENV_PREFIX] >>> "
read user_venv_prefix
if [[ $user_venv_prefix != "" ]]; then
case "$user_venv_prefix" in
*\ * )
echo "ERROR: Cannot install into directories with spaces" >&2
exit 1
;;
*)
eval VENV_PREFIX="$user_venv_prefix"
;;
esac
fi
echo -n "
Python venv will install following libraries:
$PYTHON_LIBRARIES
- Press ENTER to confirm the Python libraries
- Press CTRL-C to abort the installation
- Or specify a different Python libraries
[$PYTHON_LIBRARIES] >>> "
read venv_libraries
if [[ $venv_libraries != "" ]]; then
PYTHON_LIBRARIES="$venv_libraries"
fi
echo -n "
We will now downlaod and install Python, review settings:
miniconda url: $MC_URL
miniconda prefix: $MC_PREFIX
venv prefix: $VENV_PREFIX
python libraries: $PYTHON_LIBRARIES
- Press ENTER to confirm and continue
- Press CTRL-C to abort the installation
>>> "
read dummy
fi # end interactive mode
# check if miniconda directory already exists
if [[ ($MC_REUSE == 0) && (-e $MC_PREFIX) ]]; then
echo "ERROR: File or directory already exists: $MC_PREFIX" >&2
exit 1
fi
if [[ $MC_REUSE == 0 ]]
then
# download miniconda
wget "$MC_URL" -O "$THIS_DIR/$MC_LATEST"
# install miniconda
bash "$THIS_DIR/$MC_LATEST" -b -p "$MC_PREFIX"
# remove downloaded miniconda installation
rm "$THIS_DIR/$MC_LATEST"
# update all miniconda libraries
bash -c "source $MC_PREFIX/bin/activate && conda update --all -y && source deactivate"
fi
# create the venv
/bin/bash -c "source $MC_PREFIX/bin/activate && python -m venv --without-pip $VENV_PREFIX && echo -n conda $VENV_PREFIX && source deactivate"
# install pip inside venv
/bin/bash -c "source $VENV_PREFIX/bin/activate && curl https://bootstrap.pypa.io/get-pip.py | python && deactivate"
# install python libraries
/bin/bash -c "source $VENV_PREFIX/bin/activate && pip install --upgrade $PYTHON_LIBRARIES && deactivate"
echo -n "
The venv environment has been created, to activate it source
an activate script in its bin directory, e.g. by executing:
source $VENV_PREFIX/bin/activate
"