当前位置: 首页 > news >正文

[Bug | Cursor] import error: No module named ‘data‘

import error: No module named ‘data’

Folder Structure

  • root folder
    • data folder
      • dataloader.py
    • src folder
      • train.py <- where we try to import the dataloader.py

Failed Script

ROOT_DIR = Path(__file__).parent.parent
os.chdir(ROOT_DIR)
print(f"Using root directory: {ROOT_DIR}")from data.dataloader import seq_collate

It is right to set the ROOT_DIR and calling os.chdir(ROOT_DIR), but changing the working directory via os.chdir() does not affect Python’s module resolution (sys.path).

🔍 Why your current approach failed

  • os.chdir(ROOT_DIR) affects where relative files are opened (open(), etc.)
  • But from data... uses sys.path (which defaults to the directory the script is launched from)
  • So data wasn’t found in any of the directories listed in sys.path

✅ Final Fix: Explicitly Add ROOT_DIR to sys.path

# Define the root dir of your project
ROOT_DIR = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT_DIR))  # ← THIS IS THE FIX
print(f"Using root directory: {ROOT_DIR}")from data.dataloader import seq_collate
http://www.lryc.cn/news/597022.html

相关文章:

  • C++刷题 - 7.23
  • 【C++】类和对象(中)构造函数、析构函数
  • nrm指南
  • 二级建造师学习笔记-2025
  • 2025 成都航空装备展供需发布:精准匹配,高效成交
  • 货车手机远程启动功能的详细使用步骤及注意事项
  • C#值类型属性的典型问题
  • 基于.Net Core开源的库存订单管理系统
  • 【NLP舆情分析】基于python微博舆情分析可视化系统(flask+pandas+echarts) 视频教程 - 主页-微博点赞量Top6实现
  • 粗大误差智能滤除:基于格拉布斯准则与机器学习的数据清洗体系​
  • 深入理解 TCP 协议:Linux 网络传输的可靠基石
  • 【Node.js】使用ts-node运行ts文件时报错: TypeError: Unknown file extension “.ts“ for ts 文件
  • Node.js 倒计时图片服务部署与 Nginx 反向代理实战总结
  • The History of Computers
  • 用 Phi-3 Mini 4K Instruct 实现轻量级模型量化与加载
  • WWDC 25 给自定义 SwiftUI 视图穿上“玻璃外衣”:最新 Liquid Glass 皮肤详解
  • 漫画机器学习播客对话图文版
  • OpenHarmony BUILD.gn中执行脚本
  • 趣玩-Ollama-Llm-Chatrbot
  • 第四章 Freertos物联网实战DHT11温湿度模块
  • 利用aruco标定板标定相机
  • EDoF-ToF: extended depth of field time-of-flight imaging解读, OE 2021
  • C Primer Plus 第6版 编程练习——第10章(上)
  • 2025暑期—05神经网络-BP网络
  • 深入解析预训练语言模型在文本生成中的革命性应用:技术全景与未来挑战
  • 工业微控制器的启动过程以及安全设计所面临的挑战
  • TODAY()-WEEKDAY(TODAY(),2)+1
  • 数据结构系列之二叉搜索树
  • 关于针对 DT_REG 出现红色波浪线的问题(编译错误/IDE警告),以下是 精准解决方案,保持你的代码功能完全不变:
  • LeetCode11~20题解