9

I wonder if there is a way to open the Python console of QGIS 2.6 directly at program launch. There is a possibility to set a shortcut for the Python console, but I can't find such an option for the QGIS program launch.

  • There is a way to write a file that gets executed when QGIS starts, which can be helpful for customized settings like these. The question is whether you can open the console programmaticaly or not. Do you know if you can do that? – BritishSteel Feb 2 '15 at 11:00
  • I've searched in the API but the only class that is related to python is the QgsPythonRunner, wich is not the console. – Martin Feb 2 '15 at 11:21
10

You can start QGIS Python console when opening a project by writing a couple of lines in QGIS->Project->Project Properties:

def openProject():
    import qgis     
    qgis.utils.iface.actionShowPythonDialog().trigger()

Make sure you enable macros on your project, this way: Settings->Options->General->Enable macros: Always

As you want the QGIS Python console to open when launching QGIS, you can create (if it doesn't exist already) a startup.py file in .qgis2/python/ and write:

import qgis     
qgis.utils.iface.actionShowPythonDialog().trigger()

EDIT (to address a follow-up question by @Miro)

As pointed out by Miro, if QGIS Python Console is open, qgis.utils.iface.actionShowPythonDialog().trigger() will close it, so, if we are writing a QGIS plugin, it might make sense to know if the Python Console is open (visible) or not.

You can know if the Python Console is not visible (and then open it) by running this code:

from PyQt4.QtGui import QDockWidget
pythonConsole = iface.mainWindow().findChild( QDockWidget, 'PythonConsole' )
if not pythonConsole.isVisible():
    pythonConsole.setVisible( True )
  • Is there a way to check if console is already open? Because if it is the trigger() is going to close it. – Miro Sep 30 '16 at 5:16
  • @Miro, there is a way! I've edited my answer with instructions. – Germán Carrillo Oct 2 '16 at 12:52
  • 1
    In 2.18, the project macro works but the startup script fails to open the console. – underdark Nov 22 '16 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.