I'm using PyCharm with QGIS3 and Python3. I tried this code to import a shape file and the other to connect to PostGIS vector layer. The codes goes right on the python console of QGIS, but in the PcCharm environment it says me "the layer is not valid". I don't understand why. It seems I can't read the address.
First code:
from qgis.core import *
local_path = '/home/nicola/Scrivania/layers/ate_argillla.shp'
#vlayer = QgsVectorLayer(os.path.abspath(local_path), 'insediativo_50000', 'ogr')
vlayer = QgsVectorLayer('/home/nicola/Scrivania/layers/ate_argillla.shp', 'argille', 'ogr')
if not vlayer.isValid():
print("Il layer non è valido")
else:
print("Il layer è valido")
print(vlayer)
Second code:
from qgis.core import *
uri = QgsDataSourceUri()
uri.setConnection("localhost", "5432", "ptcp", "postgres", "aaa")
uri.setDataSource("public", "franelineari", "geom")
vlayer1 = QgsVectorLayer(uri.uri(False), "alvei fluviali", "postgres")
if vlayer1.isValid():
print("il layer è valido")
else:
print("layer is not valid")