@@ -142,9 +142,47 @@ export async function promptForMissingTool(
142142 * @param pyPackage name of python package in PyPi
143143 */
144144export async function pipInstall ( pyPackage : string ) : Promise < string > {
145- const py = 'python3' ; // Fetches the top-most python in the Shell
145+ const py = await checkPython ( ) ;
146+
146147 const args = [ '-m' , 'pip' , 'install' , '--user' , '--upgrade' , pyPackage ] ;
147- return await shellTask ( py , args , `pip: ${ pyPackage } ` ) ;
148+ return await shellTask ( py , args , `python3 -m pip install ${ pyPackage } ` ) ;
149+ }
150+
151+ /**
152+ * Checks whether python can be called from the shell.
153+ *
154+ * Tries `python` on Windows and `python3` on other platforms.
155+ *
156+ * TODO: this could also check for python version, which has to be > 3.7 for fortls.
157+ *
158+ * @returns name of the command to run python on the current platform
159+ */
160+ export async function checkPython ( ) : Promise < string > {
161+ let py = "" ;
162+ if ( os . platform ( ) == "win32" ) {
163+ py = 'python' ;
164+ } else {
165+ py = 'python3' ;
166+ }
167+ const args = [ '--version' ] ;
168+
169+ try {
170+ await shellTask ( py , args , 'getting python version' ) ;
171+ return py ;
172+ } catch ( e ) {
173+ let errMsg = "" ;
174+ if ( os . platform ( ) == "win32" ) {
175+ errMsg = py + " isn't callable from the shell. " +
176+ "Please make sure python is installed and added to the PATH." ;
177+ } else {
178+ errMsg = py + " isn't callable from the shell. Please make sure python is installed" ;
179+ }
180+
181+ return await new Promise < string > ( ( result , reject ) => {
182+ reject ( errMsg ) ;
183+ } ) ;
184+ }
185+
148186}
149187
150188export async function shellTask ( command : string , args : string [ ] , name : string ) : Promise < string > {
0 commit comments