Python console: Difference between revisions

From FreeCAD Documentation
(Moved sentence to the introduction and add a link to the Preferences Editor.)
(Removed the duplicate information from the Notes paragraph. Please don't do this. It is BAD practice.)
 
(18 intermediate revisions by 5 users not shown)
Line 1: Line 1:
<languages/>
<languages/>
<translate>
<translate>
<!--T:1-->
{{VeryImportantMessage|(January 2020) FreeCAD was originally designed to work with Python 2. Since Python 2 reached end of life in 2020, future development of FreeCAD will be done exclusively with Python 3, and backwards compatibility will not be supported.}}
{{VeryImportantMessage|(January 2020) FreeCAD was originally designed to work with Python 2. Since Python 2 reached end of life in 2020, future development of FreeCAD will be done exclusively with Python 3, and backwards compatibility will not be supported.}}


== Introduction ==
== Introduction == <!--T:2-->


</translate>
The [[Python console|Python console]] is a panel that runs an instance of the [[Python|Python]] interpreter which can be used to control FreeCAD processes, and create and modify objects and their properties.
{{TOCright}}
<translate>

<!--T:3-->
The [[Python console|Python console]] is a panel that's part of the FreeCAD [[interface|user interface]]. It runs an instance of the [[Python|Python]] interpreter which can be used to control FreeCAD processes, and create and modify objects and their properties.

<!--T:22-->
It can be made visible/hidden through the {{MenuCommand|View → Panels → Python console}} drop-down menu.

<!--T:4-->
The Python console in FreeCAD has basic syntax highlighting, able to differentiate with various styles and colors, comments, strings, numeric values, built in functions, printed text output, and delimiters like parentheses and commas. These properties of the console can be configured in the [[Preferences_Editor|Preferences editor]].


<!--T:5-->
[[File:FreeCAD_Python_console.png|800px]]
[[File:FreeCAD_Python_console.png|800px]]


<!--T:6-->
{{Caption|The Python console showing messages when FreeCAD has just started.}}
{{Caption|The Python console showing messages when FreeCAD has just started.}}


== Scripting == <!--T:7-->
The Python console in FreeCAD has basic syntax highlighting, able to distinguish with styles and colors comments, strings, numeric values, built in functions, printed text output, and delimiters like parentheses and commas. Certain properties of the console can be configured in the [[Preferences_Editor|Preferences Editor]].

== Scripting ==


<!--T:8-->
{{Emphasis|For absolute beginners, see:}} [[Introduction to Python|Introduction to Python]], and [[Python scripting tutorial|Python scripting tutorial]].
{{Emphasis|For absolute beginners, see:}} [[Introduction to Python|Introduction to Python]], and [[Python scripting tutorial|Python scripting tutorial]].


<!--T:9-->
{{Emphasis|See also:}} [[FreeCAD Scripting Basics|FreeCAD scripting basics]], and [[Scripted objects|Scripted objects]].
{{Emphasis|See also:}} [[FreeCAD Scripting Basics|FreeCAD scripting basics]], and [[Scripted objects|Scripted objects]].


<!--T:10-->
The Python console can perform basic code completion when a dot is written after an object; it will show public methods and attributes (variables) of the current object (class), for example, {{incode|obj.}}
The Python console can perform basic code completion when a dot is written after an object; it will show public methods and attributes (variables) of the current object (class), for example, {{incode|obj.}}


<!--T:11-->
The console is also able to show the documentation string of a particular function when the opening parenthesis is written, for example, {{incode|function(}}
The console is also able to show the documentation string of a particular function when the opening parenthesis is written, for example, {{incode|function(}}


<!--T:12-->
[[File:FreeCAD_Python_console_example.png|800px]]
[[File:FreeCAD_Python_console_example.png|800px]]


<!--T:13-->
{{Caption|Example Python code that produces objects in the 3D view.}}
{{Caption|Example Python code that produces objects in the 3D view.}}


<!--T:14-->

The Python console automatically loads some modules, and defines some aliases.
The FreeCAD initialization scripts automatically load some modules, and define some aliases. Therefore, in the Python console these are available
</translate>
</translate>
{{Code|code=
{{Code|code=
Line 36: Line 54:
<translate>
<translate>


<!--T:15-->
Therefore these are equal
Therefore these are equal

</translate>
</translate>
{{Code|code=
{{Code|code=
Line 44: Line 64:
<translate>
<translate>


<!--T:19-->
== Actions ==
{{Emphasis|Note:}} these pre-loaded modules and aliases are only available from the Python console embedded inside the FreeCAD program. If you use FreeCAD as a library in an external program, you must remember to load the {{incode|FreeCAD}} and {{incode|FreeCADGui}} modules and define the necessary aliases if you wish.


== Actions == <!--T:16-->

<!--T:17-->
Right click on the Python console shows some commands:
Right click on the Python console shows some commands:
* {{MenuCommand|Copy}}: stores the selected text in the clipboard for later pasting; it is disabled if nothing is selected.
* {{MenuCommand|Copy}}: stores the selected text in the clipboard for later pasting; it is disabled if nothing is selected.
Line 57: Line 81:
* {{MenuCommand|Word wrap}}: wrap very long lines that exceed the horizontal dimension of the Python console.
* {{MenuCommand|Word wrap}}: wrap very long lines that exceed the horizontal dimension of the Python console.


== Notes == <!--T:20-->
{{Userdocnavi}}

<!--T:21-->
* One has the ability to scroll the API in the Python console. Example:
*# In the console type: {{incode|FreeCAD.}}
*# A dialog box will display with optional classes/functions to choose from
*# Scroll through the list to read the description of each class/function
*# By choosing a function and following it with a {{incode|.}} one can repeat steps 2 and 3 to traverse deeper in to the API
* Tab/Word completion is supported using the {{KEY|Ctrl}}+{{KEY|Space}} shortcut


</translate>
</translate>
{{Interface navi{{#translation:}}}}
{{Std Base navi{{#translation:}}}}
{{Userdocnavi{{#translation:}}}}

Latest revision as of 10:35, 17 February 2022

(January 2020) FreeCAD was originally designed to work with Python 2. Since Python 2 reached end of life in 2020, future development of FreeCAD will be done exclusively with Python 3, and backwards compatibility will not be supported.

Introduction

The Python console is a panel that's part of the FreeCAD user interface. It runs an instance of the Python interpreter which can be used to control FreeCAD processes, and create and modify objects and their properties.

It can be made visible/hidden through the View → Panels → Python console drop-down menu.

The Python console in FreeCAD has basic syntax highlighting, able to differentiate with various styles and colors, comments, strings, numeric values, built in functions, printed text output, and delimiters like parentheses and commas. These properties of the console can be configured in the Preferences editor.

The Python console showing messages when FreeCAD has just started.

Scripting

For absolute beginners, see: Introduction to Python, and Python scripting tutorial.

See also: FreeCAD scripting basics, and Scripted objects.

The Python console can perform basic code completion when a dot is written after an object; it will show public methods and attributes (variables) of the current object (class), for example, obj.

The console is also able to show the documentation string of a particular function when the opening parenthesis is written, for example, function(

Example Python code that produces objects in the 3D view.

The FreeCAD initialization scripts automatically load some modules, and define some aliases. Therefore, in the Python console these are available

App = FreeCAD
Gui = FreeCADGui

Therefore these are equal

App.newDocument()
FreeCAD.newDocument()

Note: these pre-loaded modules and aliases are only available from the Python console embedded inside the FreeCAD program. If you use FreeCAD as a library in an external program, you must remember to load the FreeCAD and FreeCADGui modules and define the necessary aliases if you wish.

Actions

Right click on the Python console shows some commands:

  • Copy: stores the selected text in the clipboard for later pasting; it is disabled if nothing is selected.
  • Copy command: stores the selected command in the clipboard for later pasting; it is disabled if nothing is selected.
  • Copy history: copy the entire history of Python commands entered in this session.
  • Save history as: save the entire history of Python commands entered in this session to a text file.
  • Paste: paste previously copied text in the clipboard to the Python console.
  • Select all: selects all text in the Python console.
  • Clear console: erases all commands entered into the Python console. This is useful if the Python console is full of messages and previously entered commands that may be distracting when testing a new function. This is merely aesthetic, as this command doesn't delete existing variables nor clears the imported modules in the session.
  • Insert file name: opens a dialog to search for a file in the system, then it inserts the full path of the file. This is useful to test functions that process an input file, without having to write the entire name in the console, which is error prone. This command does not run the file, and does not import it as a Python module, it just returns the full path of that file.
  • Word wrap: wrap very long lines that exceed the horizontal dimension of the Python console.

Notes

  • One has the ability to scroll the API in the Python console. Example:
    1. In the console type: FreeCAD.
    2. A dialog box will display with optional classes/functions to choose from
    3. Scroll through the list to read the description of each class/function
    4. By choosing a function and following it with a . one can repeat steps 2 and 3 to traverse deeper in to the API
  • Tab/Word completion is supported using the Ctrl+Space shortcut