Skip to content

Commit a75b883

Browse files
authored
Merge pull request #465 from Flow-Launcher/DetectPythonPath
Use where.exe to detect pythonPath easily
2 parents c7358f0 + 456016c commit a75b883

File tree

1 file changed

+48
-32
lines changed

1 file changed

+48
-32
lines changed

Flow.Launcher.Core/Plugin/PluginsLoader.cs

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Flow.Launcher.Infrastructure.UserSettings;
1212
using Flow.Launcher.Plugin;
1313
using Flow.Launcher.Plugin.SharedCommands;
14+
using System.Diagnostics;
15+
using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch;
1416

1517
namespace Flow.Launcher.Core.Plugin
1618
{
@@ -85,11 +87,7 @@ public static IEnumerable<PluginPair> DotNetPlugins(List<PluginMetadata> source)
8587
return;
8688
}
8789

88-
plugins.Add(new PluginPair
89-
{
90-
Plugin = plugin,
91-
Metadata = metadata
92-
});
90+
plugins.Add(new PluginPair {Plugin = plugin, Metadata = metadata});
9391
});
9492
metadata.InitTime += milliseconds;
9593
}
@@ -123,24 +121,42 @@ public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source,
123121
// PATH or from the given pythonDirectory
124122
if (string.IsNullOrEmpty(settings.PythonDirectory))
125123
{
126-
var paths = Environment.GetEnvironmentVariable(PATH);
127-
if (paths != null)
124+
var whereProcess = Process.Start(new ProcessStartInfo
125+
{
126+
FileName = "where.exe",
127+
Arguments = "pythonw",
128+
RedirectStandardOutput = true,
129+
CreateNoWindow = true,
130+
UseShellExecute = false
131+
});
132+
133+
var pythonPath = whereProcess?.StandardOutput.ReadToEnd().Trim();
134+
135+
if (!string.IsNullOrEmpty(pythonPath))
128136
{
129-
var pythonInPath = paths
137+
pythonPath = FilesFolders.GetPreviousExistingDirectory(FilesFolders.LocationExists, pythonPath);
138+
}
139+
140+
if (string.IsNullOrEmpty(pythonPath))
141+
{
142+
var paths = Environment.GetEnvironmentVariable(PATH);
143+
144+
pythonPath = paths?
130145
.Split(';')
131-
.Where(p => p.ToLower().Contains(Python))
132-
.Any();
146+
.FirstOrDefault(p => p.ToLower().Contains(Python));
147+
}
133148

134-
if (pythonInPath)
135-
{
136-
Constant.PythonPath =
137-
Path.Combine(paths.Split(';').Where(p => p.ToLower().Contains(Python)).FirstOrDefault(), PythonExecutable);
138-
settings.PythonDirectory = FilesFolders.GetPreviousExistingDirectory(FilesFolders.LocationExists, Constant.PythonPath);
139-
}
140-
else
141-
{
142-
Log.Error("PluginsLoader", "Failed to set Python path despite the environment variable PATH is found", "PythonPlugins");
143-
}
149+
if (!string.IsNullOrEmpty(pythonPath))
150+
{
151+
Constant.PythonPath = Path.Combine(pythonPath, PythonExecutable);
152+
settings.PythonDirectory =
153+
FilesFolders.GetPreviousExistingDirectory(FilesFolders.LocationExists, Constant.PythonPath);
154+
}
155+
else
156+
{
157+
Log.Error("PluginsLoader",
158+
"Failed to set Python path despite the environment variable PATH is found",
159+
"PythonPlugins");
144160
}
145161
}
146162
else
@@ -153,17 +169,17 @@ public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source,
153169
else
154170
{
155171
Log.Error("PluginsLoader", $"Tried to automatically set from Settings.PythonDirectory " +
156-
$"but can't find python executable in {path}", "PythonPlugins");
172+
$"but can't find python executable in {path}", "PythonPlugins");
157173
}
158174
}
159175

160176
if (string.IsNullOrEmpty(settings.PythonDirectory))
161177
{
162178
if (MessageBox.Show("Flow detected you have installed Python plugins, " +
163-
"would you like to install Python to run them? " +
164-
Environment.NewLine + Environment.NewLine +
165-
"Click no if it's already installed, " +
166-
"and you will be prompted to select the folder that contains the Python executable",
179+
"would you like to install Python to run them? " +
180+
Environment.NewLine + Environment.NewLine +
181+
"Click no if it's already installed, " +
182+
"and you will be prompted to select the folder that contains the Python executable",
167183
string.Empty, MessageBoxButtons.YesNo) == DialogResult.No
168184
&& string.IsNullOrEmpty(settings.PythonDirectory))
169185
{
@@ -196,7 +212,8 @@ public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source,
196212
DroplexPackage.Drop(App.python3_9_1).Wait();
197213

198214
var installedPythonDirectory =
199-
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Programs\Python\Python39");
215+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
216+
@"Programs\Python\Python39");
200217
var pythonPath = Path.Combine(installedPythonDirectory, PythonExecutable);
201218
if (FilesFolders.FileExists(pythonPath))
202219
{
@@ -214,7 +231,8 @@ public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source,
214231

215232
if (string.IsNullOrEmpty(settings.PythonDirectory))
216233
{
217-
MessageBox.Show("Unable to set Python executable path, please try from Flow's settings (scroll down to the bottom).");
234+
MessageBox.Show(
235+
"Unable to set Python executable path, please try from Flow's settings (scroll down to the bottom).");
218236
Log.Error("PluginsLoader",
219237
$"Not able to successfully set Python path, the PythonDirectory variable is still an empty string.",
220238
"PythonPlugins");
@@ -226,8 +244,7 @@ public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source,
226244
.Where(o => o.Language.ToUpper() == AllowedLanguage.Python)
227245
.Select(metadata => new PluginPair
228246
{
229-
Plugin = new PythonPlugin(Constant.PythonPath),
230-
Metadata = metadata
247+
Plugin = new PythonPlugin(Constant.PythonPath), Metadata = metadata
231248
})
232249
.ToList();
233250
}
@@ -238,9 +255,8 @@ public static IEnumerable<PluginPair> ExecutablePlugins(IEnumerable<PluginMetada
238255
.Where(o => o.Language.ToUpper() == AllowedLanguage.Executable)
239256
.Select(metadata => new PluginPair
240257
{
241-
Plugin = new ExecutablePlugin(metadata.ExecuteFilePath),
242-
Metadata = metadata
258+
Plugin = new ExecutablePlugin(metadata.ExecuteFilePath), Metadata = metadata
243259
});
244260
}
245261
}
246-
}
262+
}

0 commit comments

Comments
 (0)