1

嗨,在启动 QGIS3 时,我的 startup.py 脚本有以下代码,我想检查用户配置文件是否存在,如果不存在,则创建并加载它,但似乎这个过程陷入了某种不可阻挡的循环,使其无法使用该应用程序。

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)

有没有办法解决这个问题?

4

1 回答 1

0

谢谢你的问题,因为这对我有帮助。我稍微修改了您的代码以利用它。

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

defaultsti = QgsApplication.qgisSettingsDirPath() #path to profiles
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

于 2022-01-25T15:37:25.393 回答