Merge remote-tracking branch 'origin/master' into 2044-share-button
[arvados.git] / doc / sdk / python / sdk-python.html.textile.liquid
1 ---
2 layout: default
3 navsection: sdk
4 navmenu: Python
5 title: "Python SDK"
6
7 ...
8
9 The Python SDK provides a generic set of wrappers so you can make API calls easily. It performs some validation before connecting to the API server: for example, it refuses to do an API call if a required parameter is missing.
10
11 The library also includes some conveniences for use in Crunch scripts; see "Crunch utility libraries":crunch-utility-libraries.html for details.
12
13 h3. Installation
14
15 If you are logged in to an Arvados VM, the Python SDK should be installed.
16
17 To use the Python SDK elsewhere, you can either install the Python SDK via PyPI or build and install the package using the arvados source tree.
18
19 {% include 'notebox_begin' %}
20 The Python SDK requires Python 2.7
21 {% include 'notebox_end' %}
22
23 h4. Option 1: install with PyPI
24
25 <notextile>
26 <pre>
27 $ <code class="userinput">sudo apt-get install python-pip python-dev libattr1-dev libfuse-dev pkg-config python-yaml</code>
28 $ <code class="userinput">sudo pip install arvados-python-client</code>
29 </pre>
30 </notextile>
31
32 _If your version of @pip@ is 1.4 or newer, the @pip install@ command might give an error: "Could not find a version that satisfies the requirement arvados-python-client". If this happens, fix it by adding a @--pre@ flag:_
33
34 <notextile>
35 <pre>
36 $ <code class="userinput">sudo pip install --pre arvados-python-client</code>
37 </pre>
38 </notextile>
39
40 h4. Option 2: build and install from source
41
42 <notextile>
43 <pre>
44 ~$ <code class="userinput">sudo apt-get install python-dev libattr1-dev libfuse-dev pkg-config</code>
45 ~$ <code class="userinput">git clone https://github.com/curoverse/arvados.git</code>
46 ~$ <code class="userinput">cd arvados/sdk/python</code>
47 ~/arvados/sdk/python$ <code class="userinput">sudo python setup.py install</code>
48 </pre>
49 </notextile>
50
51 h4. Test installation
52
53 If the SDK is installed and your @ARVADOS_API_HOST@ and @ARVADOS_API_TOKEN@ environment variables are set up correctly (see "api-tokens":{{site.baseurl}}/user/reference/api-tokens.html for details), @import arvados@ should produce no errors:
54
55 <notextile>
56 <pre>$ <code class="userinput">python</code>
57 Python 2.7.4 (default, Sep 26 2013, 03:20:26) 
58 [GCC 4.7.3] on linux2
59 Type "help", "copyright", "credits" or "license" for more information.
60 >>> <code class="userinput">import arvados</code>
61 >>> <code class="userinput">arvados.api('v1')</code>
62 &lt;apiclient.discovery.Resource object at 0x233bb50&gt;
63 </pre>
64 </notextile>
65
66 h3. Examples
67
68 Get the User object for the current user:
69
70 <notextile>
71 <pre><code class="userinput">current_user = arvados.api('v1').users().current().execute()
72 </code></pre>
73 </notextile>
74
75 Get the UUID of an object that was retrieved using the SDK:
76
77 <notextile>
78 <pre><code class="userinput">my_uuid = current_user['uuid']
79 </code></pre>
80 </notextile>
81
82 Retrieve an object by ID:
83
84 <notextile>
85 <pre><code class="userinput">some_user = arvados.api('v1').users().get(uuid=my_uuid).execute()
86 </code></pre>
87 </notextile>
88
89 Create an object:
90
91 <notextile>
92 <pre><code class="userinput">test_link = arvados.api('v1').links().create(
93     body={'link_class':'test','name':'test'}).execute()
94 </code></pre>
95 </notextile>
96
97 Update an object:
98
99 <notextile>
100 <pre><code class="userinput">arvados.api('v1').links().update(
101     uuid=test_link['uuid'],
102     body={'properties':{'foo':'bar'}}).execute()
103 </code></pre>
104 </notextile>
105
106 Get a list of objects:
107
108 <notextile>
109 <pre><code class="userinput">repos = arvados.api('v1').repositories().list().execute()
110 len(repos['items'])</code>
111 2
112 <code class="userinput">repos['items'][0]['uuid']</code>
113 u'qr1hi-s0uqq-kg8cawglrf74bmw'
114 </code></pre>
115 </notextile>
116
117 h3. Notes
118
119 The general form of an API call is:
120
121 <notextile>
122 <pre><code class="userinput">arvados.api(<i>api_version</i>).<i>plural_resource_type</i>().<i>api_method</i>(<i>parameter</i>=<i>value</i>, ...).execute()
123 </code></pre>
124 </notextile>
125
126 Many API methods accept a parameter whose name is the same as the resource type. For example, @links.create@ accepts a parameter called @link@. This parameter should be given as @body@.
127
128 <notextile>
129 <pre><code class="userinput">arvados.api('v1').links().create(
130     uuid=test_link['uuid'],
131     body={'properties':{'foo':'bar'}}).execute()
132 </code></pre>
133 </notextile>
134
135 One way to make API calls slightly less verbose is:
136
137 <notextile>
138 <pre><code class="userinput">arv = arvados.api('v1')
139 j = arv.jobs().list().execute()
140 </code></pre>
141 </notextile>
142
143 The SDK retrieves the list of API methods from the server at run time. Therefore, the set of available methods is determined by the server version rather than the SDK version.
144