如何解决MLFlow UI出现的FileNotFoundError: [WinError 2]错误?
MLFlow UI启动失败:FileNotFoundError [WinError 2]
问题背景
我通过以下代码创建MLFlow跟踪文件夹并记录指标、参数:
dir_parent = os.getcwd() dir_mlflow = 'file:' + os.sep + os.path.join(dir_parent, 'mlflow_test') mlflow.set_tracking_uri(dir_mlflow) # 记录参数到mlflow mlflow.log_param('batch_size', args.batch_size) mlflow.log_param('epochs', args.epochs) mlflow.log_param('learning_rate', args.learning_rate) # 定义回调函数跟踪训练过程指标 class LogMetricsCallback(keras.callbacks.Callback): def on_epoch_end(self, epoch, logs={}): mlflow.log_metric('training_loss', logs['loss'], epoch) mlflow.log_metric('training_accuracy', logs['accuracy'], epoch) # 获取测试集指标 test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2) mlflow.log_metric('test_loss', test_loss) mlflow.log_metric('test_accuracy', test_acc) # 将模型保存到mlflow mlflow.keras.log_model(model, artifact_path = 'keras-model')
执行后生成mlflow_test文件夹,内含.trash、0、mlruns三个子文件夹。我进入该文件夹后执行mlflow ui或mlflow ui --host 0.0.0.0命令尝试查看运行记录,触发以下错误:
Traceback (most recent call last): File "C:\Program Files\Python39\lib\runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Program Files\Python39\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "C:\Users\PJ\AppData\Roaming\Python\Python39\site-packages\mlflow\__main__.py", line 3, in <module> cli.main() File "C:\Users\PJ\AppData\Roaming\Python\Python39\site-packages\click\core.py", line 1055, in main rv = self.invoke(ctx) File "C:\Users\PJ\AppData\Roaming\Python\Python39\site-packages\click\core.py", line 1657, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "C:\Users\PJ\AppData\Roaming\Python\Python39\site-packages\click\core.py", line 1404, in invoke return ctx.invoke(self.callback, **ctx.params) File "C:\Users\PJ\AppData\Roaming\Python\Python39\site-packages\click\core.py", line 760, in invoke return __callback(*args, **kwargs) File "C:\Users\PJ\AppData\Roaming\Python\Python39\site-packages\mlflow\cli.py", line 399, in server _run_server( File "C:\Users\PJ\AppData\Roaming\Python\Python39\site-packages\mlflow\server\__init__.py", line 161, in _run_server _exec_cmd(full_command, extra_env=env_map, capture_output=False) File "C:\Users\PJ\AppData\Roaming\Python\Python39\site-packages\mlflow\utils\process.py", line 95, in _exec_cmd process = subprocess.Popen( File "C:\Program Files\Python39\lib\subprocess.py", line 951, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Program Files\Python39\lib\subprocess.py", line 1420, in _execute_child hp, ht, pid, tid = _winapi.CreateProcess(executable, args, FileNotFoundError: [WinError 2] The system cannot find the file specified
环境信息
- MLFlow版本:2.1.1
- Python版本:3.9.5 64位
- 操作系统:Windows 10
解决方法
1. 对齐Python环境与MLFlow路径
- 执行
where python查看当前命令行使用的Python路径,再执行pip show mlflow确认MLFlow安装的site-packages路径是否匹配,确保使用的是同一环境。 - 如果用了虚拟环境,先激活对应环境再运行
mlflow ui。
2. 显式指定跟踪URI启动UI
不用进入文件夹,直接指定运行记录的存储路径启动:
mlflow ui --backend-store-uri ./mlflow_test/mlruns
或使用完整绝对路径:
mlflow ui --backend-store-uri "C:\你的完整路径\mlflow_test\mlruns"
3. 替换兼容Windows的服务器
MLFlow默认用的gunicorn不支持Windows,改用waitress:
- 先安装waitress:
pip install waitress
- 启动UI时指定使用waitress:
mlflow ui --backend-store-uri ./mlflow_test/mlruns --serve-backend waitress
4. 升级MLFlow版本
MLFlow 2.1.1存在Windows下的已知兼容性问题,升级到2.3.0及以上版本可修复:
pip install --upgrade mlflow
内容的提问来源于stack exchange,提问作者PJ_
相关产品推荐
相关产品推荐

