9c0ec475b4328eda8875b161bbb77795ddbb2d53
[arvados.git] / doc / sdk / python / sdk-python.html.textile.liquid
1 ---
2 layout: default
3 navsection: sdk
4 navmenu: Python
5 title: "Installation"
6 ...
7 {% comment %}
8 Copyright (C) The Arvados Authors. All rights reserved.
9
10 SPDX-License-Identifier: CC-BY-SA-3.0
11 {% endcomment %}
12
13 The Python SDK provides access from Python to the Arvados API and Keep.  It also includes a number of command line tools for using and administering Arvados and Keep, and some conveniences for use in Crunch scripts; see "Crunch utility libraries":crunch-utility-libraries.html for details.
14
15 h2. Installation
16
17 If you are logged in to an Arvados VM, the Python SDK should be installed.
18
19 To use the Python SDK elsewhere, you can install from PyPI or a distribution package.
20
21 The Python SDK supports Python 2.7 and 3.4+
22
23 h3. Option 1: Install with pip
24
25 This installation method is recommended to make the SDK available for use in your own Python programs. It can coexist with the system-wide installation method from a distribution package (option 2, below).
26
27 Run @pip install arvados-python-client@ in an appropriate installation environment, such as a virtualenv.
28
29 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 install --pre arvados-python-client@.
30
31 h3. Option 2: Install from a distribution package
32
33 This installation method is recommended to make the CLI tools available system-wide. It can coexist with the installation method described in option 1, above.
34
35 First, "add the appropriate package repository for your distribution":{{ site.baseurl }}/install/install-manual-prerequisites.html#repos.
36
37 On Red Hat-based systems:
38
39 <notextile>
40 <pre><code>~$ <span class="userinput">sudo yum install python-arvados-python-client</code>
41 </code></pre>
42 </notextile>
43
44 On Debian-based systems:
45
46 <notextile>
47 <pre><code>~$ <span class="userinput">sudo apt-get install python-arvados-python-client</code>
48 </code></pre>
49 </notextile>
50
51 h3. 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 If you installed with pip (option 1, above):
56
57 <notextile>
58 <pre>~$ <code class="userinput">python</code>
59 Python 2.7.4 (default, Sep 26 2013, 03:20:26)
60 [GCC 4.7.3] on linux2
61 Type "help", "copyright", "credits" or "license" for more information.
62 >>> <code class="userinput">import arvados</code>
63 >>> <code class="userinput">arvados.api('v1')</code>
64 &lt;apiclient.discovery.Resource object at 0x233bb50&gt;
65 </pre>
66 </notextile>
67
68 If you installed from a distribution package (option 2): the package includes a virtualenv, which means the correct Python environment needs to be loaded before the Arvados SDK can be imported. This can be done by activating the virtualenv first:
69
70 <notextile>
71 <pre>~$ <code class="userinput">source /usr/share/python2.7/dist/python-arvados-python-client/bin/activate</code>
72 (python-arvados-python-client) ~$ <code class="userinput">python</code>
73 Python 2.7.4 (default, Sep 26 2013, 03:20:26)
74 [GCC 4.7.3] on linux2
75 Type "help", "copyright", "credits" or "license" for more information.
76 >>> <code class="userinput">import arvados</code>
77 >>> <code class="userinput">arvados.api('v1')</code>
78 &lt;apiclient.discovery.Resource object at 0x233bb50&gt;
79 </pre>
80 </notextile>
81
82 Or alternatively, by using the Python executable from the virtualenv directly:
83
84 <notextile>
85 <pre>~$ <code class="userinput">/usr/share/python2.7/dist/python-arvados-python-client/bin/python</code>
86 Python 2.7.4 (default, Sep 26 2013, 03:20:26)
87 [GCC 4.7.3] on linux2
88 Type "help", "copyright", "credits" or "license" for more information.
89 >>> <code class="userinput">import arvados</code>
90 >>> <code class="userinput">arvados.api('v1')</code>
91 &lt;apiclient.discovery.Resource object at 0x233bb50&gt;
92 </pre>
93 </notextile>
94
95 h3. Examples
96
97 Get the User object for the current user:
98
99 <notextile>
100 <pre><code class="userinput">current_user = arvados.api('v1').users().current().execute()
101 </code></pre>
102 </notextile>
103
104 Get the UUID of an object that was retrieved using the SDK:
105
106 <notextile>
107 <pre><code class="userinput">my_uuid = current_user['uuid']
108 </code></pre>
109 </notextile>
110
111 Retrieve an object by ID:
112
113 <notextile>
114 <pre><code class="userinput">some_user = arvados.api('v1').users().get(uuid=my_uuid).execute()
115 </code></pre>
116 </notextile>
117
118 Create an object:
119
120 <notextile>
121 <pre><code class="userinput">test_link = arvados.api('v1').links().create(
122     body={'link_class':'test','name':'test'}).execute()
123 </code></pre>
124 </notextile>
125
126 Update an object:
127
128 <notextile>
129 <pre><code class="userinput">arvados.api('v1').links().update(
130     uuid=test_link['uuid'],
131     body={'properties':{'foo':'bar'}}).execute()
132 </code></pre>
133 </notextile>
134
135 Get a list of objects:
136
137 <notextile>
138 <pre><code class="userinput">repos = arvados.api('v1').repositories().list().execute()
139 len(repos['items'])</code>
140 2
141 <code class="userinput">repos['items'][0]['uuid']</code>
142 u'qr1hi-s0uqq-kg8cawglrf74bmw'
143 </code></pre>
144 </notextile>
145
146 h3. Notes
147
148 The general form of an API call is:
149
150 <notextile>
151 <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()
152 </code></pre>
153 </notextile>
154
155 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@.
156
157 <notextile>
158 <pre><code class="userinput">arvados.api('v1').links().create(
159     uuid=test_link['uuid'],
160     body={'properties':{'foo':'bar'}}).execute()
161 </code></pre>
162 </notextile>
163
164 One way to make API calls slightly less verbose is:
165
166 <notextile>
167 <pre><code class="userinput">arv = arvados.api('v1')
168 j = arv.jobs().list().execute()
169 </code></pre>
170 </notextile>
171
172 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.