I wrote a code that work well in console python, an example:
from qgis.core import QgsProject, QgsLayerTreeGroup, QgsLayerTreeLayer, QgsLayerTree
from PyQt5.QtCore import Qt, QVariant, QSettings, QTranslator, qVersion,
QCoreApplication
from PyQt5 import QtGui
from PyQt5.QtGui import QAction, QIcon, QFileDialog, QProgressDialog, QProgressBar, QShortcut, QKeySequence
from osgeo import ogr
from shutil import copyfile
from urllib.request import urlopen
import re
import processing, glob, os, time
newLyrName1 = '1_PdR da controllare 4326'
newLyrName2 = '2_PdR attivi da controllare 4326'
#definisco il progressivo delle operazioni
m=0
n=0
#Definisco il nome dei campi da utilizzare come variabili
fieldX = 'X_COORD'
fieldY = 'Y_COORD'
m=m+1
n=0
#creo un popup che richiede una risposta
msgBOX = QtGui.QMessageBox.question(iface.mainWindow(), '',#titolo
str(m) + " - Importare i PdR da file excel?", #messaggi
QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) #pulsanti
And I saved it in a file called "importXLS.py" stored in a folder "script".
Then I built a plugin with a custom toolbar.
Now, what I want to do is that when I press a button it call and execute a pyqgis script inside a console python but, for now, I can only open a console python but I cannot execute nothing inside it.