Bump loofah from 2.2.3 to 2.3.1 in /apps/workbench
[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 The SDK uses @pycurl@ which depends on the @libcurl@ C library.  To build the module you may have to install additional packages.  On Debian 9 this is:
30
31 <pre>
32 $ apt-get install git build-essential python3-dev libcurl4-openssl-dev libssl1.0-dev
33 </pre>
34
35 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@.
36
37 h3. Option 2: Install from a distribution package
38
39 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.
40
41 First, "add the appropriate package repository for your distribution":{{ site.baseurl }}/install/install-manual-prerequisites.html#repos.
42
43 On Red Hat-based systems:
44
45 <notextile>
46 <pre><code>~$ <span class="userinput">sudo yum install python-arvados-python-client</code>
47 </code></pre>
48 </notextile>
49
50 On Debian-based systems:
51
52 <notextile>
53 <pre><code>~$ <span class="userinput">sudo apt-get install python-arvados-python-client</code>
54 </code></pre>
55 </notextile>
56
57 h3. Test installation
58
59 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.
60
61 If you installed with pip (option 1, above):
62
63 <notextile>
64 <pre>~$ <code class="userinput">python</code>
65 Python 2.7.4 (default, Sep 26 2013, 03:20:26)
66 [GCC 4.7.3] on linux2
67 Type "help", "copyright", "credits" or "license" for more information.
68 >>> <code class="userinput">import arvados</code>
69 >>> <code class="userinput">arvados.api('v1')</code>
70 &lt;apiclient.discovery.Resource object at 0x233bb50&gt;
71 </pre>
72 </notextile>
73
74 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:
75
76 <notextile>
77 <pre>~$ <code class="userinput">source /usr/share/python2.7/dist/python-arvados-python-client/bin/activate</code>
78 (python-arvados-python-client) ~$ <code class="userinput">python</code>
79 Python 2.7.4 (default, Sep 26 2013, 03:20:26)
80 [GCC 4.7.3] on linux2
81 Type "help", "copyright", "credits" or "license" for more information.
82 >>> <code class="userinput">import arvados</code>
83 >>> <code class="userinput">arvados.api('v1')</code>
84 &lt;apiclient.discovery.Resource object at 0x233bb50&gt;
85 </pre>
86 </notextile>
87
88 Or alternatively, by using the Python executable from the virtualenv directly:
89
90 <notextile>
91 <pre>~$ <code class="userinput">/usr/share/python2.7/dist/python-arvados-python-client/bin/python</code>
92 Python 2.7.4 (default, Sep 26 2013, 03:20:26)
93 [GCC 4.7.3] on linux2
94 Type "help", "copyright", "credits" or "license" for more information.
95 >>> <code class="userinput">import arvados</code>
96 >>> <code class="userinput">arvados.api('v1')</code>
97 &lt;apiclient.discovery.Resource object at 0x233bb50&gt;
98 </pre>
99 </notextile>
100
101 h3. Examples
102
103 Get the User object for the current user:
104
105 <notextile>
106 <pre><code class="userinput">current_user = arvados.api('v1').users().current().execute()
107 </code></pre>
108 </notextile>
109
110 Get the UUID of an object that was retrieved using the SDK:
111
112 <notextile>
113 <pre><code class="userinput">my_uuid = current_user['uuid']
114 </code></pre>
115 </notextile>
116
117 Retrieve an object by ID:
118
119 <notextile>
120 <pre><code class="userinput">some_user = arvados.api('v1').users().get(uuid=my_uuid).execute()
121 </code></pre>
122 </notextile>
123
124 Create an object:
125
126 <notextile>
127 <pre><code class="userinput">test_link = arvados.api('v1').links().create(
128     body={'link_class':'test','name':'test'}).execute()
129 </code></pre>
130 </notextile>
131
132 Update an object:
133
134 <notextile>
135 <pre><code class="userinput">arvados.api('v1').links().update(
136     uuid=test_link['uuid'],
137     body={'properties':{'foo':'bar'}}).execute()
138 </code></pre>
139 </notextile>
140
141 Get a list of objects:
142
143 <notextile>
144 <pre><code class="userinput">repos = arvados.api('v1').repositories().list().execute()
145 len(repos['items'])</code>
146 2
147 <code class="userinput">repos['items'][0]['uuid']</code>
148 u'qr1hi-s0uqq-kg8cawglrf74bmw'
149 </code></pre>
150 </notextile>
151
152 h3. Notes
153
154 The general form of an API call is:
155
156 <notextile>
157 <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()
158 </code></pre>
159 </notextile>
160
161 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@.
162
163 <notextile>
164 <pre><code class="userinput">arvados.api('v1').links().create(
165     uuid=test_link['uuid'],
166     body={'properties':{'foo':'bar'}}).execute()
167 </code></pre>
168 </notextile>
169
170 One way to make API calls slightly less verbose is:
171
172 <notextile>
173 <pre><code class="userinput">arv = arvados.api('v1')
174 j = arv.jobs().list().execute()
175 </code></pre>
176 </notextile>
177
178 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.