---
layout: page
title: "Python 2 & 3 Interpreter for Apache Zeppelin"
description: "Python is a programming language that lets you work quickly and integrate systems more effectively."
group: interpreter
---
{% include JB/setup %}
# Python 2 & 3 Interpreter for Apache Zeppelin
| Property |
Default |
Description |
| zeppelin.python |
python |
Path of the installed Python binary (could be python2 or python3).
You should set this property explicitly if python is not in your $PATH(example: /usr/bin/python).
|
| zeppelin.python.maxResult |
1000 |
Max number of dataframe rows to display. |
| zeppelin.python.useIPython |
true |
When this property is true, %python would be delegated to %python.ipython if IPython is available, otherwise
IPython is only used in %python.ipython.
|
| zeppelin.yarn.dist.archives |
|
Comma separated list of archives to be extracted into the working directory of interpreter. e.g. You can specify conda pack archive files via this property in python's yarn mode. It could be either files in local filesystem or files on hadoop compatible file systems |
| zeppelin.interpreter.conda.env.name |
|
conda environment name, aka the folder name in the working directory of interpreter |
## Vanilla Python Interpreter (`%python`)
The vanilla python interpreter provides basic python interpreter feature, only python installed is required.
### Matplotlib integration
The vanilla python interpreter can display matplotlib figures inline automatically using the `matplotlib`:
```python
%python
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
```
The output of this command will by default be converted to HTML by implicitly making use of the `%html` magic. Additional configuration can be achieved using the builtin `z.configure_mpl()` method. For example,
```python
z.configure_mpl(width=400, height=300, fmt='svg')
plt.plot([1, 2, 3])
```
Will produce a 400x300 image in SVG format, which by default are normally 600x400 and PNG respectively.
In the future, another option called `angular` can be used to make it possible to update a plot produced from one paragraph directly from another
(the output will be `%angular` instead of `%html`). However, this feature is already available in the `pyspark` interpreter.
More details can be found in the included "Zeppelin Tutorial: Python - matplotlib basic" tutorial notebook.
If Zeppelin cannot find the matplotlib backend files (which should usually be found in `$ZEPPELIN_HOME/interpreter/lib/python`) in your `PYTHONPATH`,
then the backend will automatically be set to agg, and the (otherwise deprecated) instructions below can be used for more limited inline plotting.
If you are unable to load the inline backend, use `z.show(plt)`:
```python
%python
import matplotlib.pyplot as plt
plt.figure()
(.. ..)
z.show(plt)
plt.close()
```
The `z.show()` function can take optional parameters to adapt graph dimensions (width and height) as well as output format (png or optionally svg).
```python
%python
z.show(plt, width='50px')
z.show(plt, height='150px', fmt='svg')
```
| API |
Description |
| z.put(key, value) |
Put object value with identifier key to distributed resource pool of Zeppelin,
so that it can be used by other interpreters |
| z.get(key) |
Get object with identifier key from distributed resource pool of Zeppelin |
| z.remove(key) |
Remove object with identifier key from distributed resource pool of Zeppelin |
| z.getAsDataFrame(key) |
Get object with identifier key from distributed resource pool of Zeppelin and converted into pandas dataframe.
The object in the distributed resource pool must be table type, e.g. jdbc interpreter result.
|
| z.angular(name, noteId = None, paragraphId = None) |
Get the angular object with identifier name |
| z.angularBind(name, value, noteId = None, paragraphId = None) |
Bind value to angular object with identifier name |
| z.angularUnbind(name, noteId = None) |
Unbind value from angular object with identifier name |
| z.show(p) |
Show python object p in Zeppelin, if it is pandas dataframe, it would be displayed in Zeppelin's table format,
others will be converted to string |
| z.textbox(name, defaultValue="") |
Create dynamic form Textbox name with defaultValue |
| z.select(name, options, defaultValue="") |
Create dynamic form Select name with options and defaultValue. options should be a list of Tuple(first element is key,
the second element is the displayed value) e.g. z.select("f2",[("o1","1"),("o2","2")],"o1") |
| z.checkbox(name, options, defaultChecked=[]) |
Create dynamic form Checkbox `name` with options and defaultChecked. options should be a list of Tuple(first element is key,
the second element is the displayed value) e.g. z.checkbox("f3", [("o1","1"), ("o2","2")],["o1"]) |
| z.noteTextbox(name, defaultValue="") |
Create note level dynamic form Textbox |
| z.noteSelect(name, options, defaultValue="") |
Create note level dynamic form Select |
| z.noteCheckbox(name, options, defaultChecked=[]) |
Create note level dynamic form Checkbox |
| z.run(paragraphId) |
Run paragraph |
| z.run(noteId, paragraphId) |
Run paragraph |
| z.runNote(noteId) |
Run the whole note |
## Run Python in yarn cluster
Zeppelin supports to run python interpreter in yarn cluster which means the python interpreter runs in the yarn container.
This can achieve better multi-tenant for python interpreter especially when you already have a hadoop yarn cluster.
But there's one critical problem to run python in yarn cluster: how to manage the python environment in yarn container. Because yarn cluster is a distributed cluster environemt
which is composed many nodes, and your python interpreter can start in any node. It is not practical to manage python environment in each nodes.
So in order to run python in yarn cluster, we would suggest you to use conda to manage your python environment, and Zeppelin can ship your
codna environment to yarn container, so that each python interpreter can has its own python environment.
### Step 1
We would suggest you to use conda pack to create archives of conda environments, and ship it to yarn container. Otherwise python interpreter
will use the python executable in PATH of yarn container.
Here's one example of yml file which could be used to generate a conda environment with python 3 and some useful python libraries.
* Create yml file for conda environment, write the following content into file `env_python_3.yml`
```text
name: python_3_env
channels:
- conda-forge
- defaults
dependencies:
- python=3.7
- pycodestyle
- numpy
- pandas
- scipy
- grpcio
- protobuf
- pandasql
- ipython
- ipykernel
- jupyter_client
- panel
- pyyaml
- seaborn
- plotnine
- hvplot
- intake
- intake-parquet
- intake-xarray
- altair
- vega_datasets
- pyarrow
```
* Create conda environment via this yml file using either `conda` or `mamba`
```bash
conda env create -f env_python_3.yml
```
```bash
mamba env create -f python_3_env
```
* Pack the conda environment using either `conda`
```bash
conda pack -n python_3
```
### Step 2
Specify the following properties to enable yarn mode for python interpreter, and specify the correct python environment.
```
zeppelin.interpreter.launcher yarn
zeppelin.yarn.dist.archives /home/hadoop/python_3.tar.gz#environment
zeppelin.interpreter.conda.env.name environment
```
`zeppelin.yarn.dist.archives` is the python conda environment tar which is created in step 1.
This tar will be shipped to yarn container and untar in the working directory of yarn container.
`environment` in `/home/hadoop/python_3.tar.gz#environment` is the folder name after untar. This folder name should be the same as `zeppelin.interpreter.conda.env.name`.
## Python environments (used for non-yarn mode)
### Default
By default, PythonInterpreter will use python command defined in `zeppelin.python` property to run python process.
The interpreter can use all modules already installed (with pip, easy_install...)
### Conda
[Conda](http://conda.pydata.org/) is an package management system and environment management system for python.
`%python.conda` interpreter lets you change between environments.
#### Usage
- get the Conda Information:
```
%python.conda info
```
- list the Conda environments:
```
%python.conda env list
```
- create a conda enviornment:
```
%python.conda create --name [ENV NAME]
```
- activate an environment (python interpreter will be restarted):
```
%python.conda activate [ENV NAME]
```
- deactivate
```
%python.conda deactivate
```
- get installed package list inside the current environment
```
%python.conda list
```
- install package
```
%python.conda install [PACKAGE NAME]
```
- uninstall package
```
%python.conda uninstall [PACKAGE NAME]
```
### Docker
`%python.docker` interpreter allows PythonInterpreter creates python process in a specified docker container.
#### Usage
- activate an environment
```
%python.docker activate [Repository]
%python.docker activate [Repository:Tag]
%python.docker activate [Image Id]
```
- deactivate
```
%python.docker deactivate
```