generated from isamplesorg/python_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_setup.md.html
163 lines (163 loc) · 16.8 KB
/
python_setup.md.html
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang xml:lang>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<meta name="author" content="Dave" />
<meta name="dcterms.date" content="2021-05-24" />
<title>Python setup on OS X and Linux</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="data:text/css,body%20%7B%0Adisplay%3A%20block%3B%0Atext%2Dalign%3A%20left%3B%0Amax%2Dwidth%3A%2040rem%3B%0Apadding%2Dleft%3A%200%2E5rem%3B%0Apadding%2Dright%3A%200%2E5rem%3B%0Amargin%3A%20auto%3B%0A%7D%0A%0Apre%2EnumberSource%20a%2EsourceLine%20%7B%0Aposition%3A%20relative%3B%0Aleft%3A%20%2D3em%3B%0A%7D%0Apre%2EnumberSource%20a%2EsourceLine%3A%3Abefore%7B%0Acontent%3A%20attr%28title%29%3B%0Aposition%3A%20relative%3B%0Aleft%3A%20%2D1em%3B%0Atext%2Dalign%3A%20right%3B%0Avertical%2Dalign%3A%20baseline%3B%0Aborder%3A%20none%3B%0Apointer%2Devents%3A%20all%3B%0Adisplay%3A%20inline%2Dblock%3B%0A%2Dwebkit%2Dtouch%2Dcallout%3A%20none%3B%0A%2Dwebkit%2Duser%2Dselect%3A%20none%3B%0A%2Dkhtml%2Duser%2Dselect%3A%20none%3B%0A%2Dmoz%2Duser%2Dselect%3A%20none%3B%0A%2Dms%2Duser%2Dselect%3A%20none%3B%0Auser%2Dselect%3A%20none%3B%0Apadding%3A%200%204px%3B%0Awidth%3A%203em%3B%0Acolor%3A%20%23aaaaaa%3B%0A%7D%0Apre%2EnumberSource%20%7B%0Amargin%2Dleft%3A%202em%3B%0Aborder%2Dleft%3A%201px%20solid%20%23aaaaaa%3B%0Apadding%2Dleft%3A%204px%3B%0A%7D%0A%0Adiv%2EexampleMarkup%3Abefore%7B%0Acontent%3A%20%22Markdown%22%3B%0Afont%2Dfamily%3A%20monospace%3B%0Adisplay%3A%20block%3B%0Aheight%3A%202em%3B%0Afont%2Dsize%3A%20150%25%3B%0A%7D%0Adiv%2EexampleMarkup%20%7B%0Apadding%2Dtop%3A%200%2E5em%3B%0Apadding%2Dleft%3A%200%2E5em%3B%0Apadding%2Dright%3A%200%2E5em%3B%0Amargin%2Dtop%3A%202em%3B%0Amargin%2Dbottom%3A%201em%3B%0Abackground%2Dcolor%3A%20honeydew%3B%0Aborder%2Dstyle%3A%20solid%3B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dcolor%3A%20limegreen%3B%0Aborder%2Dradius%3A%200%2E2em%3B%0A%7D%0Adiv%2EexampleOutput%3Abefore%7B%0Acontent%3A%20%22Output%22%3B%0Afont%2Dfamily%3A%20monospace%3B%0Adisplay%3A%20block%3B%0Afont%2Dsize%3A%20150%25%3B%0A%7D%0Adiv%2EexampleOutput%20%7B%0Acolor%3A%20black%3B%0Apadding%2Dtop%3A%200%2E5em%3B%0Apadding%2Dleft%3A%200%2E5em%3B%0Apadding%2Dright%3A%200%2E5em%3B%0Amargin%2Dtop%3A%201em%3B%0Amargin%2Dbottom%3A%202em%3B%0Abackground%2Dcolor%3A%20whitesmoke%3B%0Aborder%2Dstyle%3A%20solid%3B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dcolor%3A%20lightgray%3B%0Aborder%2Dradius%3A%200%2E2em%3B%0A%7D%0Adiv%2EsourceCode%20%7B%0Apadding%3A%200%2E5em%3B%0Abackground%2Dcolor%3A%20aliceblue%3B%0Aborder%2Dstyle%3A%20solid%3B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dcolor%3A%20lightskyblue%3B%0Aborder%2Dradius%3A%200%2E2em%3B%0A%7D%0Adiv%2EexampleOutput%20div%2EsourceCode%3Abefore%7B%0Acontent%3A%20%22code%22%3B%0Afont%2Dfamily%3A%20monospace%3B%0Adisplay%3A%20block%3B%0Aheight%3A%202em%3B%0Afont%2Dweight%3A%20bold%3B%0Afont%2Dsize%3A%20120%25%3B%0A%7D%0Apre%2Estdout%3Abefore%7B%0Acontent%3A%20%22stdout%22%3B%0Adisplay%3A%20block%3B%0Aheight%3A%202em%3B%0Afont%2Dweight%3A%20bold%3B%0Afont%2Dsize%3A%20120%25%3B%0A%7D%0Apre%2Estdout%20%7B%0Acolor%3A%20black%3B%0Apadding%3A%200%2E5em%3B%0Abackground%2Dcolor%3A%20azure%3B%0Aborder%2Dstyle%3A%20solid%3B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dcolor%3A%20turquoise%3B%0Aborder%2Dradius%3A%200%2E2em%3B%0Apadding%2Dleft%3A%200%2E5em%3B%0A%7D%0Acode%2Estdout%20%7B%0Acolor%3A%20black%3B%0Abackground%2Dcolor%3A%20azure%3B%0Aborder%2Dstyle%3A%20solid%3B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dcolor%3A%20turquoise%3B%0A%7D%0Apre%2Estderr%3Abefore%7B%0Acontent%3A%20%22stderr%22%3B%0Adisplay%3A%20block%3B%0Aheight%3A%202em%3B%0Afont%2Dweight%3A%20bold%3B%0Afont%2Dsize%3A%20120%25%3B%0A%7D%0Apre%2Estderr%20%7B%0Acolor%3A%20red%3B%0Apadding%3A%200%2E5em%3B%0Abackground%2Dcolor%3A%20lavenderblush%3B%0Aborder%2Dstyle%3A%20solid%3B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dcolor%3A%20pink%3B%0Aborder%2Dradius%3A%200%2E2em%3B%0Apadding%2Dleft%3A%200%2E5em%3B%0A%7D%0Acode%2Estderr%20%7B%0Acolor%3A%20red%3B%0Abackground%2Dcolor%3A%20lavenderblush%3B%0Aborder%2Dstyle%3A%20solid%3B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dcolor%3A%20pink%3B%0A%7D%0Apre%2EsourceError%3Abefore%7B%0Acontent%3A%20%22Source%20error%22%3B%0Adisplay%3A%20block%3B%0Aheight%3A%202em%3B%0Afont%2Dweight%3A%20bold%3B%0Afont%2Dsize%3A%20120%25%3B%0A%7D%0Apre%2EsourceError%20%7B%0Acolor%3A%20red%3B%0Apadding%3A%200%2E5em%3B%0Abackground%2Dcolor%3A%20lavenderblush%3B%0Aborder%2Dstyle%3A%20solid%3B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dcolor%3A%20pink%3B%0Aborder%2Dradius%3A%200%2E2em%3B%0Apadding%2Dleft%3A%200%2E5em%3B%0A%7D%0Acode%2EsourceError%20%7B%0Acolor%3A%20red%3B%0Abackground%2Dcolor%3A%20lavenderblush%3B%0Aborder%2Dstyle%3A%20solid%3B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dcolor%3A%20pink%3B%0A%7D%0A" />
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body>
<header id="title-block-header">
<h1 class="title">Python setup on OS X and Linux</h1>
<p class="subtitle">Avoiding frustration, some at least</p>
<p class="author">Dave</p>
<p class="date">2021-05-24</p>
</header>
<h1 id="goal">Goal</h1>
<p>My work involves development of many things with python dependencies. Different versions of python are used at times, and each project needs to be testable without pollution by other project dependencies. I also use many applications installed by Homebrew, some of which have python dependencies.</p>
<p>So the goal is to have a work environment that supports multiple versions of python virtual environments that play nicely with <code>brew</code>.</p>
<h1 id="process">Process</h1>
<p>Install the OS X developer tools. These are necessary to compile packages, libraries, and applications that are not distributed in binary form.</p>
<p>Install <code>homebrew</code>. This is used to manage installed binaries independently of the operating system. Be careful not to install arbitrary things. Consider the necessity of each<a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a>.</p>
<p>Install these packages using <code>brew</code>:</p>
<pre><code>pyenv
pyenv-virtualenvwrapper
zlib
sqlite
graphviz</code></pre>
<p>Add this to <code>.zshrc</code>:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode numberSource zsh numberLines"><code class="sourceCode zsh"><span id="cb2-1"><a href="#cb2-1"></a><span class="kw">export</span> <span class="ot">PATH=</span><span class="st">"/usr/local/share/python:</span><span class="ot">$PATH</span><span class="st">"</span></span>
<span id="cb2-2"><a href="#cb2-2"></a><span class="kw">export</span> <span class="ot">PYTHON_CONFIGURE_OPTS=</span><span class="st">"--enable-shared"</span></span>
<span id="cb2-3"><a href="#cb2-3"></a><span class="kw">export</span> <span class="ot">PYENV_ROOT=</span><span class="st">"</span><span class="ot">$HOME</span><span class="st">/.pyenv"</span></span>
<span id="cb2-4"><a href="#cb2-4"></a><span class="kw">export</span> <span class="ot">PATH=</span><span class="st">"</span><span class="ot">$PYENV_ROOT</span><span class="st">/bin:</span><span class="ot">$PATH</span><span class="st">"</span></span>
<span id="cb2-5"><a href="#cb2-5"></a><span class="kw">eval</span> <span class="st">"</span><span class="ot">$(</span>pyenv init --path<span class="ot">)</span><span class="st">"</span></span>
<span id="cb2-6"><a href="#cb2-6"></a><span class="kw">if</span> <span class="kw">command</span> -v pyenv <span class="kw">1></span>/dev/null <span class="kw">2>&1</span>; <span class="kw">then</span></span>
<span id="cb2-7"><a href="#cb2-7"></a> <span class="kw">eval</span> <span class="st">"</span><span class="ot">$(</span>pyenv init <span class="kw">-</span><span class="ot">)</span><span class="st">"</span></span>
<span id="cb2-8"><a href="#cb2-8"></a><span class="kw">fi</span></span>
<span id="cb2-9"><a href="#cb2-9"></a><span class="kw">export</span> <span class="ot">WORKON_HOME=$HOME</span>/.virtualenvs</span>
<span id="cb2-10"><a href="#cb2-10"></a>mkdir -p <span class="ot">$WORKON_HOME</span></span>
<span id="cb2-11"><a href="#cb2-11"></a><span class="kw">export</span> <span class="ot">PROJECT_HOME=$HOME</span>/Documents/Projects</span></code></pre></div>
<p>and restart the shell.</p>
<p>Next:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode numberSource zsh numberLines"><code class="sourceCode zsh"><span id="cb3-1"><a href="#cb3-1"></a>pyenv install 3.9.5</span>
<span id="cb3-2"><a href="#cb3-2"></a>pip install -U pip</span>
<span id="cb3-3"><a href="#cb3-3"></a>pip install virtualenvwrapper</span></code></pre></div>
<p>Edit <code>~/.zshrc</code> again, adding after the line <code>export PROJECT_HOME= ...</code>:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode zsh"><code class="sourceCode zsh"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>pyenv virtualenvwrapper</span></code></pre></div>
<p>and restart the shell again.</p>
<p>Running <code>python</code> should show something like:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode numberSource zsh numberLines"><code class="sourceCode zsh"><span id="cb5-1"><a href="#cb5-1"></a>% python</span>
<span id="cb5-2"><a href="#cb5-2"></a>Python 3.9.5 <span class="kw">(</span>default, May 25 2021, 06:41:57<span class="kw">)</span></span>
<span id="cb5-3"><a href="#cb5-3"></a>[Clang 12.0.5 <span class="kw">(</span>clang-1205.0.22.9<span class="kw">)</span>] on darwin</span>
<span id="cb5-4"><a href="#cb5-4"></a>Type <span class="st">"help"</span>, <span class="st">"copyright"</span>, <span class="st">"credits"</span> or <span class="st">"license"</span> <span class="kw">for</span> more information.</span></code></pre></div>
<p>You now have a functional python isolated from the system python. Homebrew can use this <code>pyenv</code> managed version of python, which can help avoid confusion if using brew install pacakges like <code>libspatiallite</code>. To setup <code>pyenv</code> python with <code>brew</code>:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode numberSource zsh numberLines"><code class="sourceCode zsh"><span id="cb6-1"><a href="#cb6-1"></a>mkdir -p <span class="ot">$(</span>brew --cellar python<span class="ot">)</span></span>
<span id="cb6-2"><a href="#cb6-2"></a>ln -s ~/.pyenv/versions/3.9.5 <span class="ot">$(</span>brew --cellar python<span class="ot">)</span>/3.9.5</span>
<span id="cb6-3"><a href="#cb6-3"></a>brew link python3</span></code></pre></div>
<p>Install some common dependencies in the shared python install:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode numberSource zsh numberLines"><code class="sourceCode zsh"><span id="cb7-1"><a href="#cb7-1"></a>pip install pipx</span>
<span id="cb7-2"><a href="#cb7-2"></a>pip install poetry</span>
<span id="cb7-3"><a href="#cb7-3"></a>pip install jupyter</span></code></pre></div>
<h1 id="virtual-environments">Virtual environments</h1>
<p>To create a virtual environment:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode zsh"><code class="sourceCode zsh"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>mkvirtualenv some_project</span></code></pre></div>
<p>To work in a virtual environment:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode zsh"><code class="sourceCode zsh"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>workon some_project</span></code></pre></div>
<p>To exit a virtual environment:</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode zsh"><code class="sourceCode zsh"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>deactivate</span></code></pre></div>
<p>To delete a virtual environment:</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode zsh"><code class="sourceCode zsh"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>rmvirtualenv some_project</span></code></pre></div>
<h1 id="jupyter-and-virtual-environments">Jupyter and virtual environments</h1>
<p>Create separate kernels for specific projects to avoid cross-project dependency pollution.</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode zsh"><code class="sourceCode zsh"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>pip install ipykernel</span></code></pre></div>
<p>From within the virtual environement of choice:</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode zsh"><code class="sourceCode zsh"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>python -m ipykernel install --user --name=some_project</span></code></pre></div>
<p>To list the kernels:</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode zsh"><code class="sourceCode zsh"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>jupyter kernelspec list</span></code></pre></div>
<p>To remove a kernel:</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode zsh"><code class="sourceCode zsh"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a>jupyter kernelspec remove some_project</span></code></pre></div>
<section class="footnotes" role="doc-endnotes">
<hr />
<ol>
<li id="fn1" role="doc-endnote"><p>Unless the goal is to create a gallimaufry.<a href="#fnref1" class="footnote-back" role="doc-backlink">↩︎</a></p></li>
</ol>
</section>
</body>
</html>