Problems with loading user profile with PyQGIS

202 Views Asked by At

Hi I have the following code for my startup.py script when starting QGIS3, that I want to check if a userprofile exists, and if not then to create and load it, but it seems like this process falls into some kind of a unstoppable loop, making it unpossible to use the application.

from qgis.core import * 
from PyQt5 import * 
import os import getpass
import qgis
defaultsti = QgsApplication.qgisSettingsDirPath() 
os.chdir(defaultsti)
os.chdir(os.path.dirname(os.getcwd()))
r = os.getcwd() 
a = QgsUserProfileManager(r).allProfiles() 
g = getpass.getuser()

if QgsUserProfileManager(r).profileExists(g): 
   import qgis
   qgis.core.QgsUserProfileManager(r).loadUserProfile(g)

else:
   import qgis
   import getpass
   g = getpass.getuser() 
   qgis.core.QgsUserProfileManager(r).createUserProfile(g) 
   QgsUserProfileManager(r).loadUserProfile(g)

Is there a way to solve this issue?

1

There are 1 best solutions below

2
On

Thank you for you question because that help me for something. I modified your code a little to exploit it.

from qgis.core import * 
from PyQt5 import * 
import os 
import getpass

defaultsti = QgsApplication.qgisSettingsDirPath() #path to current profile
os.chdir(defaultsti)
os.chdir(os.path.dirname(os.getcwd()))
r = os.getcwd() 
a = QgsUserProfileManager(r).allProfiles() #get all profiles in a list
g = getpass.getuser()

if QgsUserProfileManager(r).profileExists(g): 
   QgsUserProfileManager(r).loadUserProfile(g) #load profile if it exists
else:
   QgsUserProfileManager(r).createUserProfile(g) #create profile if doesn't exist
   QgsUserProfileManager(r).loadUserProfile(g) #then load it