-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpython_path.html
208 lines (195 loc) · 13.1 KB
/
python_path.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Course on Practical Neuroimaging in Python — Practical neuroimaging analysis</title>
<link rel="stylesheet" href="_static/nature.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.2',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<link rel="author" title="About these documents" href="about.html" />
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Practical neuroimaging, the sequel" href="schedule_2015.html" />
<link rel="prev" title="Convolution" href="on_convolution.html" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="schedule_2015.html" title="Practical neuroimaging, the sequel"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="on_convolution.html" title="Convolution"
accesskey="P">previous</a> |</li>
<big><big><big><li><a href="index.html">Home</a> | </li>
<li><a href="https://github.com/practical-neuroimaging/pna2015">Exercises</a> | </li>
<li><a href="https://calmail.berkeley.edu/manage/list/listinfo/[email protected]">Mailing list</a> | </li></big></big></big>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="putting-modules-on-the-python-path">
<h1>Putting modules on the Python path<a class="headerlink" href="#putting-modules-on-the-python-path" title="Permalink to this headline">¶</a></h1>
<p>Let’s say I have a Python module called <code class="docutils literal"><span class="pre">myfunctions.py</span></code>. I have stored this
in a directory <code class="docutils literal"><span class="pre">/Users/mb312/code</span></code>. So, the full path to the file is
<code class="docutils literal"><span class="pre">/Users/mb312/code/myfunctions.py</span></code>.</p>
<p>Here is the contents of <code class="docutils literal"><span class="pre">myfunctions.py</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="sd">""" Some useful functions that I use, usefully</span>
<span class="sd">"""</span>
<span class="k">def</span> <span class="nf">add_badly</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="o">+</span> <span class="mi">1</span>
</pre></div>
</div>
<p>I’m working in some directory <code class="docutils literal"><span class="pre">/Users/mb312/working</span></code>
(<code class="docutils literal"><span class="pre">/Users/mb312/working</span></code> is my current directory).</p>
<p>I want to be able to start Python, <code class="docutils literal"><span class="pre">import</span> <span class="pre">myfunctions</span></code>, and then
do something like:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">myfunctions</span><span class="o">.</span><span class="n">add_badly</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
</pre></div>
</div>
<p>and get the expected answer <code class="docutils literal"><span class="pre">14</span></code>.</p>
<p>If I start Python and do that:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ python
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import myfunctions
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named myfunctions
</pre></div>
</div>
<p>This is because Python does not know where to find <code class="docutils literal"><span class="pre">myfunctions.py</span></code>.</p>
<p>When Python is looking for a new module, like <code class="docutils literal"><span class="pre">myfunctions.py</span></code>, it searches
a list of directories stored as the <code class="docutils literal"><span class="pre">path</span></code> variable, in the <code class="docutils literal"><span class="pre">sys</span></code> module.</p>
<p>Here is the contents of that variable on my system:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sys</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="s1">'</span><span class="se">\n</span><span class="s1">'</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="p">))</span>
<span class="go">/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip</span>
<span class="go">/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7</span>
<span class="go">/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin</span>
<span class="go">/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac</span>
<span class="go">/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages</span>
<span class="go">/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk</span>
<span class="go">/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old</span>
<span class="go">/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload</span>
<span class="go">/Users/mb312/Library/Python/2.7/lib/python/site-packages</span>
<span class="go">/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages</span>
<span class="go">/Library/Python/2.7/site-packages</span>
</pre></div>
</div>
<p>Notice that <code class="docutils literal"><span class="pre">sys.path</span></code> is a simple list:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">type</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="p">)</span>
<span class="go"><type 'list'></span>
</pre></div>
</div>
<p>When I run the command <code class="docutils literal"><span class="pre">import</span> <span class="pre">myfunctions</span></code>, Python will look for a file
<code class="docutils literal"><span class="pre">myfunctions.py</span></code> <a class="footnote-reference" href="#on-extensions" id="id1">[1]</a> in each of the directories in the list,
starting with the first. As soon as it finds this file, it stops, and imports
it. If it does not find the file, I get the <code class="docutils literal"><span class="pre">ImportError</span></code> we saw above.</p>
<p>The <code class="docutils literal"><span class="pre">sys.path</span></code> list of directories is also called the <em>module search path</em>.
See <a class="reference external" href="https://docs.python.org/2/tutorial/modules.html#the-module-search-path">https://docs.python.org/2/tutorial/modules.html#the-module-search-path</a> for
more detail.</p>
<p>If we want to be able to <code class="docutils literal"><span class="pre">import</span> <span class="pre">myfunctions</span></code> we need to put <em>the directory
containing the module</em> into the <em>module search path</em>. In our case we want to
put the directory <code class="docutils literal"><span class="pre">/Users/mb312/code</span></code> onto the module search path.</p>
<p>There are various ways to do this, but the most direct way to do that, is to
change the value of the <code class="docutils literal"><span class="pre">sys.path</span></code> variable after starting Python.</p>
<p>Because <code class="docutils literal"><span class="pre">sys.path</span></code> is a list of strings, I can append my directory
string to the list using standard list methods, so that Python will now search
the new directory. In my case:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">'/Users/mb312/code'</span><span class="p">)</span>
</pre></div>
</div>
<p>Now we can import our custom module:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">myfunctions</span>
</pre></div>
</div>
<table class="docutils footnote" frame="void" id="on-extensions" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>Most Python code is stored in files with the extension
<code class="docutils literal"><span class="pre">.py</span></code>. You can also write <a class="reference external" href="https://docs.python.org/2/extending/extending.html">extensions</a> for Python, which
are compiled libraries that Python can also import. You can define
functions and classes in these files as you can for Python code in <code class="docutils literal"><span class="pre">.py</span></code>
files. These files have filename extensions that depend on the platform
you are running on. On Linux or OSX the extension is generally <code class="docutils literal"><span class="pre">.so</span></code>, on
Windows, the extension is often <code class="docutils literal"><span class="pre">.pyd</span></code>.</td></tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="on_convolution.html"
title="previous chapter">Convolution</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="schedule_2015.html"
title="next chapter">Practical neuroimaging, the sequel</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/python_path.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="schedule_2015.html" title="Practical neuroimaging, the sequel"
>next</a> |</li>
<li class="right" >
<a href="on_convolution.html" title="Convolution"
>previous</a> |</li>
<big><big><big><li><a href="index.html">Home</a> | </li>
<li><a href="https://github.com/practical-neuroimaging/pna2015">Exercises</a> | </li>
<li><a href="https://calmail.berkeley.edu/manage/list/listinfo/[email protected]">Mailing list</a> | </li></big></big></big>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2013-2015, Matthew Brett and J-B Poline.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.1.
</div>
</body>
</html>