<!DOCTYPE html>
<html lang="es" dir="ltr">
<head>
<base href="../../../../">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Consola interactiva de Python</title>
<link rel="shortcut icon" href="media/navigation/favicon.ico">
<link type="text/css" href="normalize.css" rel="Stylesheet">
<link type="text/css" href="prism.css" rel="Stylesheet">
<link type="text/css" href="default.css" rel="Stylesheet">
<script type="text/javascript" src="polyfills.js"></script><script type="text/javascript" src="languages.js"></script><script type="text/javascript" src="es/langnames.js"></script><script type="text/javascript" src="flexsearch.debug.js"></script><script type="text/javascript" src="prism.js"></script><script type="text/javascript" src="help2.js" defer></script><script type="text/javascript" src="a11y-toggle.js" defer></script><script type="text/javascript" src="paginathing.js" defer></script><script type="text/javascript" src="es/bookmarks.js" defer></script><script type="text/javascript" src="es/contents.js" defer></script><script type="text/javascript" src="help.js" defer></script><meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<body>
<header id="TopLeftHeader"><a class="symbol" href="es/text/shared/05/new_help.html"><div></div></a><a class="logo" href="es/text/shared/05/new_help.html"><p dir="auto">Ayuda de LibreOffice 25.2</p></a><div class="dropdowns"><div class="modules">
<button type="button" data-a11y-toggle="modules-nav" id="modules" aria-haspopup="true" aria-expanded="false" aria-controls="modules-nav">Módulo</button><nav id="modules-nav" hidden=""></nav>
</div></div></header><aside class="leftside"><input id="accordion-1" name="accordion-menu" type="checkbox"><label for="accordion-1" dir="auto">Contenido</label><div id="Contents" class="contents-treeview"></div></aside><div id="SearchFrame"><div id="Bookmarks">
<input id="search-bar" type="search" class="search" placeholder="Buscar en los marcadores del módulo elegido" dir="auto"><div class="nav-container" tabindex="0"><nav class="index" dir="auto"></nav></div>
</div></div>
<div id="DisplayArea" itemprop="softwareHelp" itemscope="true" itemtype="http://schema.org/SoftwareApplication">
<noscript><div id="WarnJS"><h1 dir="auto">Active JavaScript en el navegador para mostrar las páginas de ayuda de LibreOffice.</h1></div></noscript>
<a id="N0117"></a> <meta itemprop="keywords" content="Python console"> <meta itemprop="keywords" content="Python Interactive Shell"> <meta itemprop="keywords" content="Python,Shell">
<a id="pythonshell"></a>
<h1 id="N0118" dir="auto">
<a id="pythonshell1"></a>Ejecutar la consola interactiva de Python</h1>
<p id="N0119" class="paragraph" dir="auto">The Python interactive console, also known as Python interpreter or Python shell, provides programmers with a quick way to execute commands and try out and test code without creating a file. UNO objects introspection as well as LibreOffice Python modules documentation can be obtained from the terminal.</p>
<span id="swlnsyspython_shell_1" class="switch">
<span hidden="true" id="UNIXpython_shell_1" class="UNIX">
<h2 id="hd_id381633446811095" dir="auto">Utilización de la terminal</h2>
<div class="note">
<div class="noteicon" dir="auto"><img src="media/icon-themes/res/helpimg/note.svg" alt="note" style="width:40px;height:40px;"></div>
<div class="notetext"><p id="par_id801633601715910" dir="auto">From a LibreOffice copy included in a GNU/Linux platform, use the terminal as shown:</p></div>
</div>
<br>
<p id="par_id531633444780190" class="paragraph" dir="auto"><span class="literal">whereis</span> or <span class="literal">type</span> terminal commands help locate Python interactive console:</p>
<p id="par_id871633361808210" class="paragraph" dir="auto"><br><span class="input" data-tooltip="Pulse en el texto para copiarlo en el portapapeles">user@computer:~$ </span><span class="emph">type -p python3</span><br><span class="input" data-tooltip="Pulse en el texto para copiarlo en el portapapeles">/usr/bin/python3</span><br> <span class="input" data-tooltip="Pulse en el texto para copiarlo en el portapapeles">user@computer:~$ </span><span class="emph">/usr/bin/python3</span><br> <span class="input" data-tooltip="Pulse en el texto para copiarlo en el portapapeles">Python 3.7.5 (default, Nov 20 2019, 09:21:52)</span><br><span class="input" data-tooltip="Pulse en el texto para copiarlo en el portapapeles">[GCC 9.2.1 20191008] on linux</span><br><span class="input" data-tooltip="Pulse en el texto para copiarlo en el portapapeles">Type "help", "copyright", "credits" or "license" for more information.</span><br><span class="input" data-tooltip="Pulse en el texto para copiarlo en el portapapeles">>>> </span><span class="emph">import uno</span><br><span class="input" data-tooltip="Pulse en el texto para copiarlo en el portapapeles">>>> </span><span class="emph">dir(uno)</span><br><span class="input" data-tooltip="Pulse en el texto para copiarlo en el portapapeles">['Any', 'Bool', 'ByteSequence', 'Char', 'Enum', 'PY2', 'Type', '_ConstantGroup', '__builtin__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_builtin_import', '_component_context', '_impl_getConstantGroupByName', '_uno_extract_printable_stacktrace', '_uno_import', '_uno_struct__eq__', '_uno_struct__getattr__', '_uno_struct__init__', '_uno_struct__ne__', '_uno_struct__repr__', '_uno_struct__setattr__', '_uno_struct__str__', 'absolutize', 'createUnoStruct', 'fileUrlToSystemPath', 'generateUuid', 'getClass', 'getComponentContext', 'getConstantByName', 'getCurrentContext', 'getTypeByName', 'invoke', 'isInterface', 'os', 'pyuno', 'setCurrentContext', 'six_string_types', 'socket', 'sys', 'systemPathToFileUrl', 'traceback', 'warnings']</span><br><span class="input" data-tooltip="Pulse en el texto para copiarlo en el portapapeles">>>> </span><span class="emph">exit()</span><br><span class="input" data-tooltip="Pulse en el texto para copiarlo en el portapapeles">user@computer:~$</span></p>
</span>
</span>
<div class="note">
<div class="noteicon" dir="auto"><img src="media/icon-themes/res/helpimg/note.svg" alt="note" style="width:40px;height:40px;"></div>
<div class="notetext"><p id="par_id851633601202270" dir="auto">From a full-featured LibreOffice installed package, a Basic or Python script locates the embedded copy of Python console.</p></div>
</div>
<br>
<h2 id="N0121" dir="auto">Utilizar una macro BASIC</h2>
<p id="par_id71707747845585" class="paragraph" dir="auto">This routine resorts to <a target="_top" href="es/text/sbasic/python/python_platform.html">Platform class module</a> in order to distinguish the actual operating system.</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Pulse en el texto para copiarlo en el portapapeles"><pre dir="auto"><code class="language-visual-basic line-numbers">
Sub interpreter_console
Set opsys = New Platform
ps = CreateUnoService("com.sun.star.util.PathSettings")
install_path = ConvertFromURL(ps.Module)
If opsys.isMacOSX Then
cmd = "/System/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal "
pgm_path = Array( install_path,"..","Resources","python" )
ElseIf opsys.isLinux Then
cmd = "x-terminal-emulator -e "
pgm_path = Array(install_path,"python")
ElseIf opsys.isWindows Then
cmd = ""
pgm_path = Array("python")
EndIf
python_interpreter = Join( pgm_path, GetPathSeparator() )
Shell(cmd + python_interpreter)
End Sub
</code></pre></div>
<h2 id="N0127" dir="auto">Utilizar una macro Python</h2>
<div class="pycode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Pulse en el texto para copiarlo en el portapapeles"><pre dir="auto"><code class="language-python line-numbers">
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import uno, os, subprocess
def interpreter_console():
ctx = XSCRIPTCONTEXT.getComponentContext()
smgr = ctx.getServiceManager()
ps = smgr.createInstanceWithContext("com.sun.star.util.PathSettings", ctx)
install_path = uno.fileUrlToSystemPath(ps.Module)
pgm = install_path + os.sep + "python" # Python shell/console path
subprocess.Popen(pgm) # Start Python interactive Shell
</code></pre></div>
<h3 id="N0141" dir="auto">Salida de ejemplo</h3>
<p id="N0142" class="paragraph" dir="auto"> <img src="media/helpimg/python/python_interactive_console.png" class="genericimage" alt="Consola interactiva de Python" style="width:;height:" itemprop="image" itemscope="true" itemtype="http://schema.org/ImageObject"></p>
<h2 id="hd_id311633513620803" dir="auto">Consola alternativa</h2>
<p id="par_id81632760673283" class="paragraph" dir="auto">Utilice la consola de la <a target="_blank" href="https://extensions.libreoffice.org/extensions/apso-alternative-script-organizer-for-Python">extensión APSO</a> como alternativa:</p>
<p id="N0144" class="paragraph" dir="auto"> <img src="media/helpimg/python/python_shell.png" class="genericimage" alt="Consola de APSO" style="width:;height:" itemprop="image" itemscope="true" itemtype="http://schema.org/ImageObject"></p>
<a id="relatedtopics"></a><div class="relatedtopics">
<p class="related" itemprop="mentions" dir="auto"><a id="related"></a><span class="emph">Temas relacionados</span></p>
<div class="relatedbody" itemprop="mentions">
<div class="embedded"><p class="embedded" dir="auto"><a target="_top" href="es/text/sbasic/python/python_platform.html">Identificar el sistema operativo</a></p></div>
<p id="par_id351633599611244" class="paragraph" dir="auto"><a target="_top" href="es/text/sbasic/shared/03/sf_exception.html#PythonShell">Función <span class="literal">PythonShell</span></a> del servicio <a target="_top" href="es/text/sbasic/shared/03/sf_exception.html"><span class="literal">ScriptForge.Exception</span></a></p>
<div class="embedded"><p class="embedded" dir="auto"><a target="_top" href="es/text/sbasic/python/python_examples.html">Ejemplos de programación en Python</a></p></div>
<div class="embedded"><p class="embedded" dir="auto"><a target="_top" href="es/text/sbasic/python/main0000.html">Ayuda con las macros en Python en LibreOffice</a></p></div>
</div>
</div>
</div>
<div id="DonationFrame"></div>
<footer><h2 style="text-align: center;"><a href="https://books.libreoffice.org" target="_blank">Libros de LibreOffice</a></h2>
<div class="noteicon" dir="auto" style="display:flex;justify-content:center;flex-wrap:wrap;row-gap:15px;">
<img src="media/navigation/libo-writer.svg" alt="Writer Icon" style="width:60px;height:60px;"><img src="media/navigation/libo-calc.svg" alt="Calc Icon" style="width:60px;height:60px;"><img src="media/navigation/libo-impress.svg" alt="Impress Icon" style="width:60px;height:60px;"><img src="media/navigation/libo-draw.svg" alt="Draw Icon" style="width:60px;height:60px;"><img src="media/navigation/libo-base.svg" alt="Base Icon" style="width:60px;height:60px;"><img src="media/navigation/libo-math.svg" alt="Math Icon" style="width:60px;height:60px;"><img src="media/navigation/libo-symbol-black.svg" alt="Getting Started Icon" style="width:60px;height:60px;">
</div>
<div id="DEBUG" class="debug">
<h3 class="bug">Help content debug info:</h3>
<p dir="auto">This page is: <a href="https://opengrok.libreoffice.org/xref/help/source/text/sbasic/python/python_shell.xhp" target="_blank">/text/sbasic/python/python_shell.xhp</a></p>
<p dir="auto">Title is: Consola interactiva de Python</p>
<p id="bm_module" dir="auto"></p>
<p id="bm_system" dir="auto"></p>
<p id="bm_HID" dir="auto"></p>
</div></footer>
</body>
</html>