Google Colab导入flareml_utils等模块失败的问题求助
解决Google Colab中
flareml_utils及相关模块导入失败问题 问题背景
在Google Colab中运行课程项目代码时,出现两类导入错误:
- 自定义模块
flareml_utils、flareml_train、flareml_test无法找到,尝试pip install提示无匹配包 sklearn_extensions相关类导入失败
运行代码
from google.colab import drive drive.mount('/content/drive') import sys sys.path.insert(0,'/content/drive/MyDrive/Colab Notebooks') #!pip install matplotlib==3.4.2 numpy==1.19.5 scikit-learn==0.24.2 sklearn-extensions==0.0.2 pandas==1.2.4 import warnings warnings.filterwarnings('ignore') # Data manipulation import pandas as pd import numpy as np # Training the models # The following libraries are used to train the algorithms: Random Forest, MLP, and ELM. from sklearn.preprocessing import StandardScaler from sklearn.ensemble import RandomForestClassifier from sklearn.neural_network import MLPClassifier from sklearn_extensions.extreme_learning_machines.elm import GenELMClassifier # ERROR from sklearn_extensions.extreme_learning_machines.random_layer import RBFRandomLayer, MLPRandomLayer # ERROR # Visualizations import matplotlib.pyplot as plt from flareml_utils import plot_custom_result # ERROR # Running the training, testing and prediction. from flareml_train import train_model # ERROR from flareml_test import test_model # ERROR
报错信息
ModuleNotFoundError Traceback (most recent call last) <ipython-input-5-fc8aafaf4a3d> in <module> 23 # Visualizations 24 import matplotlib.pyplot as plt ---> 25 from flareml_utils import plot_custom_result # ERROR 26 # Running the training, testing and prediction. 27 from flareml_train import train_model # ERROR ModuleNotFoundError: No module named 'flareml_utils'
尝试pip install flareml_utils时的错误:
ERROR: Could not find a version that satisfies the requirement flareml_utils (from versions: none) ERROR: No matching distribution found for flareml_utils
解决方案
1. 安装公开依赖包
代码中注释掉了依赖安装命令,取消注释并运行,解决sklearn_extensions的导入问题:
!pip install matplotlib==3.4.2 numpy==1.19.5 scikit-learn==0.24.2 sklearn-extensions==0.0.2 pandas==1.2.4
2. 处理自定义模块导入问题
flareml_utils、flareml_train、flareml_test不是公开PyPI包,无法通过pip安装,它们是课程项目配套的自定义Python脚本:
- 找到课程提供的
flareml_utils.py、flareml_train.py、flareml_test.py三个文件 - 将文件上传到Google Drive的
/content/drive/MyDrive/Colab Notebooks目录下(与当前Colab笔记本路径一致) - 确认代码中的路径添加正确:
sys.path.insert(0,'/content/drive/MyDrive/Colab Notebooks')已将该目录加入Python搜索路径,只要文件存在,即可成功导入
3. 额外验证步骤
- 确认Google Drive挂载成功:运行
drive.mount('/content/drive')时完成了授权,能正常访问目标目录 - 检查文件名拼写:确保上传的文件名与导入语句完全一致(Python对文件名大小写敏感)
内容的提问来源于stack exchange,提问作者Alaa Rajab
相关产品推荐
相关产品推荐

