This function provides a Python REPL in theR session, which can be usedto interactively run Python code. All code executed within the REPL isrun within the Python main module, and any generated Python objects willpersist in the Python session after the REPL is detached.
Usage
repl_python( module=NULL, quiet=getOption("reticulate.repl.quiet", default=FALSE), input=NULL)Arguments
- module
An (optional) Python module to be imported beforethe REPL is launched.
- quiet
Boolean; print a startup banner when launching the REPL? If
TRUE, the banner will be suppressed.- input
Python code to be run within the REPL. Setting this can beuseful if you'd like to drive the Python REPL programmatically.
Details
When working with R and Python scripts interactively, one can activatethe Python REPL withrepl_python(), run Python code, and later runexitto return to theR console.
Magics
A handful of magics are supported inrepl_python():
Lines prefixed with! are executed as system commands:
!cmd --arg1 --arg2: Execute arbitrary system commands
Magics start with a% prefix. Supported magics include:
%conda ...executes a conda command in the active conda environment%pip ...executes pip for the active python.%load,%loadpy,%runexecutes a python file.%system,!!executes a system command and capture output%env: read current environment variables.%env name: read environment variable 'name'.%env name=val,%env name val: set environment variable 'name' to 'val'.valelements in{}are interpolated using f-strings (required Python >= 3.6).
%cd <dir>change working directory.%cd -: change to previous working directory (as set by%cd).%cd -3: change to 3rd most recent working directory (as set by%cd).%cd -foo/bar: change to most recent working directory matching"foo/bar"regex(in history of directories set via%cd).
%pwd: print current working directory.%dhist: print working directory history.
Additionally, the output of system commands can be captured in a variable, e.g.:
x = !ls
wherex will be a list of strings, consisting ofstdout output split in"\n" (stderr is not captured).
See also
py, for accessing objects created using the Python REPL.