1313__version__ = "0.13.14.dev0"
1414
1515from IPython import get_ipython
16- from IPython .core .application import ProfileDir
1716from IPython .core .debugger import BdbQuit_excepthook
18- from IPython .core .profiledir import ProfileDirError
17+ from IPython .core .profiledir import ProfileDir , ProfileDirError
1918from IPython .paths import get_ipython_dir
2019from IPython .terminal .ipapp import TerminalIPythonApp
2120from IPython .terminal .embed import InteractiveShellEmbed
@@ -33,15 +32,50 @@ def _get_debugger_cls(ipython_profile="default"):
3332 # Build a terminal app in order to force ipython to load the
3433 # configuration
3534 ipython_dir = get_ipython_dir ()
35+ profile_dir = None
36+
37+ # First, try to find the requested profile
3638 try :
3739 profile_dir = ProfileDir .find_profile_dir_by_name (
3840 ipython_dir = ipython_dir ,
3941 name = ipython_profile ,
4042 )
41- except ProfileDirError : # fallback to default-profile
42- profile_dir = ProfileDir .find_profile_dir_by_name (
43- ipython_dir = ipython_dir ,
44- )
43+ except ProfileDirError :
44+ # Profile doesn't exist, try to create it
45+ try :
46+ profile_dir = ProfileDir .create_profile_dir_by_name (
47+ ipython_dir = ipython_dir ,
48+ name = ipython_profile ,
49+ )
50+ except (ProfileDirError , Exception ):
51+ # Creation failed, fallback to default profile
52+ profile_dir = None
53+
54+ # If we still don't have a profile_dir, try the default profile
55+ if profile_dir is None :
56+ try :
57+ # Try to find default profile
58+ profile_dir = ProfileDir .find_profile_dir_by_name (
59+ ipython_dir = ipython_dir ,
60+ name = 'default' ,
61+ )
62+ except ProfileDirError :
63+ # Default doesn't exist either, create it
64+ try :
65+ profile_dir = ProfileDir .create_profile_dir_by_name (
66+ ipython_dir = ipython_dir ,
67+ name = 'default' ,
68+ )
69+ except (ProfileDirError , Exception ):
70+ # Last resort: create profile without name
71+ profile_dir = ProfileDir .create_profile_dir_by_name (
72+ ipython_dir = ipython_dir ,
73+ )
74+
75+ # Ensure we have a profile_dir before creating TerminalIPythonApp
76+ if profile_dir is None :
77+ raise RuntimeError ("Unable to create or find any IPython profile directory" )
78+
4579 ipapp = TerminalIPythonApp (profile_dir = profile_dir )
4680
4781 # Avoid output (banner, prints)
0 commit comments