如何在Kaggle中将Notebook放置到指定的src目录?
How to Move Your Kaggle Notebook into the
src Directory Got it, let's figure out how to get your Kaggle Notebook into that src directory so your code runs properly. Here are a few straightforward methods you can try:
Method 1: Move an Existing Notebook into src
If you already have a Notebook you want to relocate:
- Open your current Notebook, then look for the Files panel on the left side (click the folder icon if it's hidden).
- Click the New Folder button, name it
src, and hit enter. - Go to the top-right corner of your Notebook, click File > Download .ipynb to save a copy to your local machine.
- Back in the Files panel, navigate into the
srcdirectory you just created. - Click Upload, select the downloaded
.ipynbfile, and wait for it to finish uploading. - Open the newly uploaded Notebook in the
srcdirectory—now its working directory will align with your code's expected path structure, so your relative imports/file references should work.
Method 2: Create a New Notebook Directly in src
If you're starting fresh or want to copy code over:
- Create the
srcdirectory in the Files panel (same step as Method 1). - Navigate into the
srcdirectory. - Click the New Notebook button in the top-right corner. Confirm the creation in the pop-up window.
- Open this new Notebook, paste your existing code into it, and run—this Notebook lives directly in
src, so your path references will be correct from the start.
Method 3: Adjust Working Directory Without Moving the Notebook
If you don't want to relocate your Notebook, you can tweak the working directory at the start of your code:
Add these lines at the very top of your Notebook:
import os # Switch to the parent directory of src (or directly to src if needed) os.chdir('/kaggle/working') # Or if src is in your input directory: # os.chdir('/kaggle/input/your-dataset-name/src')
This will make your Notebook's working directory match the path your code expects, so relative imports will resolve correctly.
内容的提问来源于stack exchange,提问作者An Ignorant Wanderer




