Python Interactive Shell

Contents

# Running Python Interactive Console

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.

## Using the Terminal

From a LibreOffice copy included in a GNU/Linux platform, use the terminal as shown:

whereis or type terminal commands help locate Python interactive console:

user@computer:~$ type -p python3
/usr/bin/python3
user@computer:~$ /usr/bin/python3
Python 3.7.5 (default, Nov 20 2019, 09:21:52)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import uno
>>> dir(uno)
['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']
>>> exit()
user@computer:~$

From a full-featured LibreOffice installed package, a Basic or Python script locates the embedded copy of Python console.

## Using a Basic macro

This routine resorts to Platform class module in order to distinguish the actual operating system.

            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

## Using a Python macro

            # -*- 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

### Example output

## Alternative console

Use APSO extension console as an alternative:

Chủ đề Liên quan

Identifying the operating system

PythonShell function in ScriptForge.Exception service

Python programming examples

LibreOffice Python Scripts Help

Please support us!