summaryrefslogtreecommitdiff
path: root/source/text/sbasic/python/python_shell.xhp
blob: 0b724b88d42a8b85e32bb978da86c59cb84ed302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?xml version="1.0" encoding="UTF-8"?>
<helpdocument version="1.0">
    <!--
    * This file is part of the LibreOffice project.
    *
    * This Source Code Form is subject to the terms of the Mozilla Public
    * License, v. 2.0. If a copy of the MPL was not distributed with this
    * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    *
    -->
    <meta>
        <topic id="python_shell" indexer="include" status="PUBLISH">
            <title id="tit" xml-lang="en-US">Python Interactive Shell</title>
            <filename>/text/sbasic/python/python_shell.xhp</filename>
        </topic>
    </meta>
    <body>
    <bookmark branch="index" id="N0117">
        <bookmark_value>Python console</bookmark_value>
        <bookmark_value>Python Interactive Shell</bookmark_value>
    </bookmark>
    <section id="pythonshell">
        <paragraph role="heading" level="1" id="N0118"><variable id="pythonshell1"><link href="text/sbasic/python/python_shell.xhp" name="python shell">Running Python Interactive Console</link></variable></paragraph>
    </section>
    <paragraph role="paragraph" id="N0119">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 %PRODUCTNAME Python modules documentation can be obtained from the terminal.</paragraph>
    <paragraph role="heading" level="2" id="N0121">Using a Basic macro:</paragraph>
    <bascode>
        <paragraph role="bascode" id="N0122" localize="false">Sub interpreter_console</paragraph>
        <paragraph role="bascode" id="N0123" localize="false">    ps = CreateUnoService(&quot;com.sun.star.util.PathSettings&quot;)</paragraph>
        <paragraph role="bascode" id="N0124" localize="false">    install_path = ConvertFromURL(ps.Module)</paragraph>
        <paragraph role="bascode" id="N0125" localize="false">    Shell( install_path + GetPathSeparator() + &quot;python&quot; )</paragraph>
        <paragraph role="bascode" id="N0126" localize="false">End Sub</paragraph>
    </bascode>
    <paragraph role="heading" level="2" id="N0127">Using a Python macro:</paragraph>
    <pycode>
        <paragraph role="pycode" id="N0128" localize="false"># -*- coding: utf-8 -*-</paragraph>
        <paragraph role="pycode" id="N0129" localize="false">from __future__ import unicode_literals</paragraph>
        <paragraph role="pycode" id="N0130" localize="false">   </paragraph>
        <paragraph role="pycode" id="N0131" localize="false">import uno, os, subprocess</paragraph>
        <paragraph role="pycode" id="N0132" localize="false">    </paragraph>
        <paragraph role="pycode" id="N0133" localize="false">def interpreter_console():</paragraph>
        <paragraph role="pycode" id="N0134" localize="false">    ctx = XSCRIPTCONTEXT.getComponentContext()</paragraph>
        <paragraph role="pycode" id="N0135" localize="false">    smgr = ctx.getServiceManager()</paragraph>
        <paragraph role="pycode" id="N0136" localize="false">    ps = smgr.createInstanceWithContext(&quot;com.sun.star.util.PathSettings&quot;, ctx)</paragraph>
        <paragraph role="pycode" id="N0137" localize="false">    install_path = uno.fileUrlToSystemPath(ps.Module)</paragraph>
        <paragraph role="pycode" id="N0138" localize="false">    pgm = install_path + os.sep + &quot;python&quot;  # Python shell/console path</paragraph>
        <paragraph role="pycode" id="N0139" localize="false">    subprocess.Popen(pgm)  # Start Python interactive Shell</paragraph>
        <paragraph role="pycode" id="N0140" localize="false">    </paragraph>
    </pycode>
    <paragraph role="heading" level="2" id="N0141">Usage:</paragraph>
    <paragraph role="paragraph" id="N0142">
        <image src="media/helpimg/python/python_shell.png" id="N0143"><alt id="N0144">Python Interactive Console</alt></image>
    </paragraph>
    <section id="relatedtopics">
        <embed href="text/sbasic/python/python_examples.xhp#pythonexamples2"/>
        <embed href="text/sbasic/python/main0000.xhp#pythonscriptshelp"/>
    </section>
</body>
</helpdocument>