VSCode Conda环境下Jupyter Notebook无法导入src模块求助
Solution for ModuleNotFoundError: No module named 'src'
Root Cause
Your VSCode working directory is set to its own install folder instead of the root of your cloned GitHub repo. Python can't locate the src module because it's not in the current working directory or Python's path.
Fix 1: Set VSCode's Working Directory to Repo Root
- Open VSCode, go to File > Open Folder, then select the root folder of your cloned repo (the one containing
README.mdandsrcon your D盘). - Verify by running this in your notebook:
It should output the path to your repo root, not the VSCode install directory. After this, your original import should work without extra code.import os print(os.getcwd())
Fix 2: Temporarily Add Repo Root to Python Path
If you can't switch the working directory right now, add this code at the very top of your ws-notebook.ipynb:
import sys import os # Replace with your actual repo root path on D盘, e.g., "D:/my-cloned-repo" repo_root = "D:/path/to/your/cloned/repo" if repo_root not in sys.path: sys.path.append(repo_root) # Now run your import from src.preprocessing import create_data
Fix 3: Persistent Solution (Install as Editable Package)
For long-term use, install your project as an editable package in your Conda environment:
- Open the Conda terminal (make sure your environment is activated).
- Navigate to your repo root:
cd D:/path/to/your/cloned/repo - Run:
pip install -e .
This registers your src module with Python, so you can import it from anywhere in the environment without adjusting paths.
内容的提问来源于stack exchange,提问作者John Tipotas
相关产品推荐
相关产品推荐

