Add 'tools/arvbox/' from commit 'd3d368758db1f4a9fa5b89f77b5ee61d68ef5b72'
[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 install from a distribution package, PyPI, or source.
18
19 {% include 'notebox_begin' %}
20 The Python SDK requires Python 2.7.
21 {% include 'notebox_end' %}
22
23 h4. Option 1: Install from distribution packages
24
25 First, "add the appropriate package repository for your distribution":{{ site.baseurl }}/install/install-manual-prerequisites.html#repos.
26
27 {% include 'note_python27_sc' %}
28
29 On Debian-based systems:
30
31 <notextile>
32 <pre><code>~$ <span class="userinput">sudo apt-get install python-arvados-python-client</code>
33 </code></pre>
34 </notextile>
35
36 On Red Hat-based systems:
37
38 <notextile>
39 <pre><code>~$ <span class="userinput">sudo yum install python27-python-arvados-python-client</code>
40 </code></pre>
41 </notextile>
42
43 h4. Option 2: Install with pip
44
45 Run @pip-2.7 install arvados-python-client@ in an appropriate installation environment, such as a virtualenv.
46
47 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, try @pip-2.7 install --pre arvados-python-client@.
48
49 h4. Option 3: Install from source
50
51 Install the @python-setuptools@ package from your distribution.  Then run the following:
52
53 <notextile>
54 <pre><code>~$ <span class="userinput">git clone https://github.com/curoverse/arvados.git</span>
55 ~$ <span class="userinput">cd arvados/sdk/python</span>
56 ~$ <span class="userinput">python2.7 setup.py install</span>
57 </code></pre>
58 </notextile>
59
60 You may optionally run the final installation command in a virtualenv, or with the @--user@ option.
61
62 h4. Test installation
63
64 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:
65
66 <notextile>
67 <pre>~$ <code class="userinput">python2.7</code>
68 Python 2.7.4 (default, Sep 26 2013, 03:20:26)
69 [GCC 4.7.3] on linux2
70 Type "help", "copyright", "credits" or "license" for more information.
71 >>> <code class="userinput">import arvados</code>
72 >>> <code class="userinput">arvados.api('v1')</code>
73 &lt;apiclient.discovery.Resource object at 0x233bb50&gt;
74 </pre>
75 </notextile>
76
77 h3. Examples
78
79 Get the User object for the current user:
80
81 <notextile>
82 <pre><code class="userinput">current_user = arvados.api('v1').users().current().execute()
83 </code></pre>
84 </notextile>
85
86 Get the UUID of an object that was retrieved using the SDK:
87
88 <notextile>
89 <pre><code class="userinput">my_uuid = current_user['uuid']
90 </code></pre>
91 </notextile>
92
93 Retrieve an object by ID:
94
95 <notextile>
96 <pre><code class="userinput">some_user = arvados.api('v1').users().get(uuid=my_uuid).execute()
97 </code></pre>
98 </notextile>
99
100 Create an object:
101
102 <notextile>
103 <pre><code class="userinput">test_link = arvados.api('v1').links().create(
104     body={'link_class':'test','name':'test'}).execute()
105 </code></pre>
106 </notextile>
107
108 Update an object:
109
110 <notextile>
111 <pre><code class="userinput">arvados.api('v1').links().update(
112     uuid=test_link['uuid'],
113     body={'properties':{'foo':'bar'}}).execute()
114 </code></pre>
115 </notextile>
116
117 Get a list of objects:
118
119 <notextile>
120 <pre><code class="userinput">repos = arvados.api('v1').repositories().list().execute()
121 len(repos['items'])</code>
122 2
123 <code class="userinput">repos['items'][0]['uuid']</code>
124 u'qr1hi-s0uqq-kg8cawglrf74bmw'
125 </code></pre>
126 </notextile>
127
128 h3. Notes
129
130 The general form of an API call is:
131
132 <notextile>
133 <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()
134 </code></pre>
135 </notextile>
136
137 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@.
138
139 <notextile>
140 <pre><code class="userinput">arvados.api('v1').links().create(
141     uuid=test_link['uuid'],
142     body={'properties':{'foo':'bar'}}).execute()
143 </code></pre>
144 </notextile>
145
146 One way to make API calls slightly less verbose is:
147
148 <notextile>
149 <pre><code class="userinput">arv = arvados.api('v1')
150 j = arv.jobs().list().execute()
151 </code></pre>
152 </notextile>
153
154 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.