@@ -51,31 +51,46 @@ def found(raise_error: bool = False) -> bool:
51
51
"""
52
52
psithon = which ("psi4" , return_bool = True )
53
53
psiapi = which_import ("psi4" , return_bool = True )
54
+ error_msg = ""
55
+ error_which = which
54
56
55
57
if psithon and not psiapi :
56
58
with popen ([which ("psi4" ), "--module" ]) as exc :
57
59
exc ["proc" ].wait (timeout = 30 )
58
60
if "module does not exist" in exc ["stderr" ]:
59
- pass # --module argument only in Psi4 DDD branch
61
+ psiapi = True # --module argument only in Psi4 DDD branch (or >=1.6) so grandfathered in
60
62
else :
61
- sys .path .append (exc ["stdout" ].split ()[- 1 ])
63
+ so , se = exc ["stdout" ], exc ["stderr" ]
64
+ error_msg = f" In particular, psi4 command found but unable to load psi4 module into sys.path. stdout: { so } , stderr: { se } "
65
+ error_which = which_import
66
+ if (so ) and (not se ) and (exc ["proc" ].returncode == 0 ):
67
+ psimod = Path (so .rstrip ()) # stdout is string & Path is tolerant, so safe op
68
+ if psimod .exists ():
69
+ sys .path .append (str (psimod ))
70
+ psiapi = which_import ("psi4" , return_bool = True )
62
71
63
72
if psiapi and not psithon :
64
- psiimport = str (Path (which_import ("psi4" )).parent .parent )
65
- env = os .environ .copy ()
66
- env ["PYTHONPATH" ] = psiimport
67
- with popen (["python" , "-c" , "import psi4; print(psi4.executable[:-5])" ], popen_kwargs = {"env" : env }) as exc :
73
+ with popen (["python" , "-c" , "import psi4; print(psi4.executable)" ]) as exc :
68
74
exc ["proc" ].wait (timeout = 30 )
69
- os .environ ["PATH" ] += os .pathsep + exc ["stdout" ].split ()[- 1 ]
70
-
71
- if psithon or psiapi :
75
+ so , se = exc ["stdout" ], exc ["stderr" ]
76
+ error_msg = f" In particular, psi4 module found but unable to load psi4 command into PATH. stdout: { so } , stderr: { se } "
77
+ # yes, everthing up to here could be got from `import psi4; psiexe = psi4.executable`. but, we try not to
78
+ # load programs/modules in the `def found` fns.
79
+ if (so ) and (not se ) and (exc ["proc" ].returncode == 0 ):
80
+ psiexe = Path (so .rstrip ()) # stdout is string & Path is tolerant, so safe op
81
+ if psiexe .exists ():
82
+ os .environ ["PATH" ] += os .pathsep + str (psiexe .parent )
83
+ psithon = which ("psi4" , return_bool = True )
84
+
85
+ if psithon and psiapi :
72
86
return True
73
87
74
- return which (
88
+ return error_which (
75
89
"psi4" ,
76
90
return_bool = True ,
77
91
raise_error = raise_error ,
78
- raise_msg = "Please install via `conda install psi4 -c psi4`. Check it's in your PATH with `which psi4`." ,
92
+ raise_msg = "Please install via `conda install psi4 -c conda-forge/label/libint_dev -c conda-forge`. Check it's in your PATH with `which psi4`."
93
+ + error_msg ,
79
94
)
80
95
81
96
def get_version (self ) -> str :
@@ -85,7 +100,9 @@ def get_version(self) -> str:
85
100
if which_prog not in self .version_cache :
86
101
with popen ([which_prog , "--version" ]) as exc :
87
102
exc ["proc" ].wait (timeout = 30 )
88
- self .version_cache [which_prog ] = safe_version (exc ["stdout" ].split ()[- 1 ])
103
+ if (exc ["proc" ].returncode != 0 ) or exc ["stderr" ]:
104
+ raise TypeError (f"Error { exc ['proc' ].returncode } retrieving Psi4 version: " + exc ["stderr" ])
105
+ self .version_cache [which_prog ] = safe_version (exc ["stdout" ].rstrip ())
89
106
90
107
candidate_version = self .version_cache [which_prog ]
91
108
0 commit comments