I wonder if there is a way to open the Python Console 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.
[GIS] Showing Python Console when QGIS starts
pyqgispythonqgisqgis-python-console
Related Solutions
You are able to access any plugin data by using the qgis.utils.plugins
from qgis import utils
mypluginInstance = utils.plugins['myplugin']
print(mypluginInstance.myData)
To automatically open the QGIS Python Console when QGIS starts, you can read How to show Python console at QGIS program start.
However, I guess your ultimate intention is to run the script (even if it doesn't run from the QGIS Python Console), so you can create (if it doesn't exist already) a startup.py
file in the QGIS Python directory:
- On GNU/Linux:
/home/USER/.qgis2/python/
- On Windows:
C:\Program Files\QGIS\python\
orC:\Documents and Settings\$USERNAME\.qgis\python\
(I don't really know about paths on Windows 10, but if you have problems finding the correct Python path, just open the Plugin manager, click on any installed 3rd party plugin and read the path where it's installed).
And then either:
Call your script from
startup.py
:import my_script my_script.run() # If that's the method you want to execute from your script
or,
Just put your script code into the
startup.py
file, which is the same as copying your Python script to the aforementioned folder and rename it tostartup.py
EDIT: Explain caveat when using the startup.py approach and suggest another approach
startup.py
will be executed everytime you open QGIS, which gives you no control on whether you want to automatically run your script or just run QGIS to work on a new project.
Therefore, I suggest you to take another approach: use QGIS project macros.
Say you have a script (myScript.py
) you want to run when QGIS starts from your schedule tasks. Copy/move the script into the QGIS Python path or into the same directory where your QGIS project will be saved.
Let's assume your script looks like this:
from qgis.utils import iface
def run():
iface.addVectorLayer("/Path/To/geodata/myShape.shp","layer1","ogr")
Open a new QGIS project and click Project-->Project Properties...-->Macros
, enable the Python macros
checkbox, and write this:
import myScript
def openProject():
myScript.run()
def saveProject():
pass
def closeProject():
pass
Click OK and go to Settings-->Options...-->General
to choose Enable macros Always
.
Save your QGIS project as, let's say, run_script_project.qgs
.
Now, every time you want to get your script automatically executed when starting QGIS, just start QGIS from the console, in this way:
qgis --project /Path/To/Your/Project/run_script_project.qgs
Once QGIS opens the project, the run()
method of your script will be executed.
Best Answer
Using macros
You can start QGIS Python console when opening a project by writing a couple of lines in
QGIS->Project->Project Properties
:Make sure you enable macros on your project, this way:
Settings->Options->General->Enable macros: Always
Using startup.py
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%APPDATA%\QGIS\QGIS3\
(e.g., GNU/Linux:/home/USER/.local/share/QGIS/QGIS3/
or Windows:C:\Users\USER\AppData\Roaming\QGIS\QGIS3\
) and write: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: