I've found many similar problems in gis.stackexchange, but no solutions worked for me.
I want to execute a python macros with QGIS 3.22.14 and python39 installed via OSGEO4W and windows 11 home. The purpose is to works a script automatically when I open a project in QGIS. The errors maybe apparently change, but there is always the same situation. The code works in the console or in the standalone version, but the code does not work as python macros.
USE OF CODE OF THE CONSOLE (A)
This is my script that works properly when I run the code in the Python Console of QGIS:
uri = "pagingEnabled='true' restrictToRequestBBOX='1' srsname='EPSG:2056' typename='ac_001_1_v1_1_disponibilita_catasto_rdpp' url='https://wfs.geo.ti.ch/?MAP=/project/wfs_ac.qgz' version='auto'"
catasto = QgsVectorLayer(uri, "ac_001_1_v1_1_disponibilita_catasto_rdpp", "WFS")
QgsProject.instance().addMapLayer(catasto)
You can see in this image what I mean for "code in the Python Console of QGIS"

I run the SAME script in the macro below def OpenProject():

The result is that and you can see that it does not work as macro:

USE OF THE STANDALONE CODE CREATED TO BE ABLE TO WORK OUTSIDE QGIS SESSION (B)
Bacause of this unsatisfatory result, I decided to run the version standalone (created followuing this guide https://docs.qgis.org/3.22/en/docs/pyqgis_developer_cookbook/intro.html#using-pyqgis-in-standalone-scripts) of the same code that has prooven to work well using a batch file. I put all the code under def openProject() hoping that the macro could work.
The script is the following:
def openProject():
from qgis.core import *
# Supply path to qgis install location
QgsApplication.setPrefixPath("C:/OSGeo4W/apps/qgis-ltr", True)
# Create a reference to the QgsApplication. Setting the
# second argument to False disables the GUI.
qgs = QgsApplication([], False)
# Load providers
qgs.initQgis()
# Write your code here to load some layers, use processing
# algorithms, etc.
from qgis.analysis import QgsNativeAlgorithms
import processing
from processing.core.Processing import Processing
Processing.initialize()*
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
uri = "pagingEnabled='true' restrictToRequestBBOX='1' srsname='EPSG:2056' typename='ac_001_1_v1_1_disponibilita_catasto_rdpp' url='https://wfs.geo.ti.ch/?MAP=/project/wfs_ac.qgz' version='auto'"
catasto = QgsVectorLayer(uri, "ac_001_1_v1_1_disponibilita_catasto_rdpp", "WFS")
QgsProject.instance().addMapLayer(catasto)
# Finally, exitQgis() is called to remove the
# provider and layer registries from memory
qgs.exitQgis()
def saveProject():
pass
def closeProject():
pass
I show the abovementioned script even as image to allow you to understand better my attempt:

Final question: How to solve this issue? Maybe the solution should be using the standalone script (B) and understand which module to add instead of * that is not accepted in the fonction. Or maybe someone find another way to simply adding something in the python code of the console (A).
