Skip to content

Commit 863de04

Browse files
committed
kernel
1 parent f94c7cb commit 863de04

File tree

4 files changed

+157
-3
lines changed

4 files changed

+157
-3
lines changed

docs/src/appendix/index.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,70 @@ code .
348348
```
349349
350350
Once VS Code is open, you can create Jupyter notebooks directly in the editor and run them with the full notebook experience. You should now be able to pick up with the [VS Code tutorial](../notebook.md) and start working from there.
351+
352+
## Troubleshooting Python Environments and Kernels
353+
354+
If you're experiencing issues with importing packages or running code, the problem is often related to Python environment or kernel selection. Here are the most common issues and solutions:
355+
356+
### "ModuleNotFoundError: No module named 'pandas'" (or other packages)
357+
358+
**Cause**: VS Code is using a different Python environment than where you installed packages.
359+
360+
**Solutions**:
361+
1. **Check Python Interpreter** (for .py files):
362+
- Press `Ctrl+Shift+P` → "Python: Select Interpreter"
363+
- Choose the interpreter where you installed packages
364+
- Verify in the status bar (bottom-left of VS Code)
365+
366+
2. **Check Jupyter Kernel** (for .ipynb files):
367+
- Click kernel name in top-right of notebook
368+
- Select the same Python environment where packages are installed
369+
- Look for kernel paths matching your installation method (uv, conda, etc.)
370+
371+
### Kernel Won't Start or Keeps Disconnecting
372+
373+
**Solutions**:
374+
1. **Refresh Python Interpreters**:
375+
- Press `Ctrl+Shift+P` → "Python: Refresh"
376+
- Try selecting kernel again
377+
378+
2. **Restart VS Code**:
379+
- Close and reopen VS Code
380+
- Reopen your notebook and select kernel
381+
382+
3. **Check Python Installation**:
383+
- Ensure Python and jupyter are properly installed
384+
- For uv users: run `uv add jupyter` in your project
385+
- For conda users: run `conda install jupyter`
386+
387+
### Multiple Python Installations Confusion
388+
389+
**If you have conda, uv, and system Python**:
390+
1. **Decide on one approach** for this tutorial
391+
2. **Install all packages in the same environment**
392+
3. **Always select the matching interpreter/kernel**
393+
4. **Use consistent commands** (don't mix `pip`, `conda`, and `uv`)
394+
395+
### Environment Path Issues
396+
397+
**Check these common locations**:
398+
- **uv projects**: Look for `.venv` folder in your project directory
399+
- **conda**: Look for `anaconda3` or `miniconda3` in paths
400+
- **system Python**: Usually `/usr/bin/python` or `/usr/local/bin/python`
401+
402+
### Quick Verification Steps
403+
404+
Before running tutorial code:
405+
1. **Open a new terminal in VS Code** (`View > Terminal`)
406+
2. **Test imports**:
407+
```python
408+
python -c "import pandas; print('✅ pandas works')"
409+
python -c "import matplotlib; print('✅ matplotlib works')"
410+
python -c "import jupyter; print('✅ jupyter works')"
411+
```
412+
3. **If any fail**: Reinstall packages in your current environment
413+
4. **Match VS Code selection**: Ensure interpreter/kernel matches this environment
414+
415+
```{tip}
416+
**Golden Rule**: The Python interpreter (status bar) and Jupyter kernel (notebook top-right) should always point to the same environment where you installed pandas, matplotlib, seaborn, and altair.
417+
```

docs/src/notebook.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ Let's start by creating your first Python file. In VS Code:
1616

1717
```{note}
1818
If VS Code prompts you to select a Python interpreter, choose the Python installation you set up in the previous chapter. This tells VS Code which Python environment to use when running your code.
19+
20+
**How to select the right interpreter:**
21+
1. Press `Ctrl+Shift+P` (or `Cmd+Shift+P` on Mac)
22+
2. Type "Python: Select Interpreter"
23+
3. Choose the Python installation where you installed packages (pandas, matplotlib, etc.)
24+
4. Look for the interpreter path that matches your setup (e.g., `.venv`, `anaconda3`, or system Python)
25+
26+
The selected interpreter will be shown in the bottom-left status bar of VS Code.
1927
```
2028

2129
Now you're ready to write your first line of Python code. VS Code provides an excellent environment for both learning and professional Python development.
@@ -195,13 +203,47 @@ In addition to interactive Python files, VS Code also supports traditional Jupyt
195203

196204
This creates a notebook file where you can add both code and markdown cells, just like in traditional Jupyter environments, but with all the benefits of VS Code's editor features.
197205

206+
### Selecting the Right Kernel
207+
208+
When you create or open a notebook, you need to select a Python kernel (the engine that runs your code):
209+
210+
1. **Automatic Selection**: VS Code may automatically select a kernel based on your workspace
211+
2. **Manual Selection**: Click the kernel name in the **top-right corner** of the notebook
212+
3. **Choose Your Kernel**: Select the Python environment where you installed your packages:
213+
- Look for the same Python path you selected as your interpreter
214+
- If using uv: Choose the kernel from your project's `.venv` folder
215+
- If using Anaconda: Choose the anaconda/miniconda kernel
216+
- The kernel name should show the Python version and environment path
217+
218+
4. **Verify Connection**: A green dot next to the kernel name means it's connected and ready to run code
219+
220+
### Common Kernel Issues and Solutions
221+
222+
**Problem**: "ModuleNotFoundError" when importing pandas or other packages
223+
224+
**Solution**:
225+
1. Check that you're using the correct kernel (top-right of notebook)
226+
2. Verify packages are installed in that environment
227+
3. Restart the kernel: Click kernel name → "Restart Kernel"
228+
229+
**Problem**: Kernel won't start or keeps disconnecting
230+
231+
**Solution**:
232+
1. Use Command Palette → "Python: Refresh"
233+
2. Try selecting a different kernel, then switch back
234+
3. Close and reopen the notebook file
235+
198236
The notebook interface in VS Code allows you to:
199237
- Add code and markdown cells
200238
- Run cells individually or all at once
201239
- View rich output including plots and tables
202240
- Export to various formats
203241
- Use VS Code's powerful editing features
204242

243+
```{tip}
244+
**Quick Check**: Before running any code, make sure the kernel name in the top-right shows the same Python environment where you installed your packages. This prevents import errors and ensures your code runs correctly.
245+
```
246+
205247
```{note}
206248
This tutorial includes a complete collection of interactive Jupyter notebooks that you can run directly in VS Code. You can find them in the [Interactive Notebooks](notebooks/index.md) section. These notebooks contain the same content as the tutorial chapters but in an interactive format where you can run the code and see the results immediately.
207249
```

docs/src/notebooks/index.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ Make sure you've completed the [VS Code Setup](../vscode_setup.md) before starti
1414

1515
1. **Download or clone** this repository to your local machine
1616
2. **Open VS Code** and navigate to the project folder
17-
3. **Open any notebook** by clicking on the `.ipynb` files in the `docs/src/notebooks/` directory
18-
4. **Run cells sequentially** by clicking the play button or pressing `Shift+Enter`
19-
5. **Experiment** by modifying the code and running your changes
17+
3. **Select the correct Python environment** in VS Code (see [setup guide](../vscode_setup.md))
18+
4. **Open any notebook** by clicking on the `.ipynb` files in the `docs/src/notebooks/` directory
19+
5. **Choose the right kernel** when prompted (should match your Python environment with packages installed)
20+
6. **Run cells sequentially** by clicking the play button or pressing `Shift+Enter`
21+
7. **Experiment** by modifying the code and running your changes
22+
23+
```{important}
24+
**Before running any notebook**: Make sure you've selected the correct Python kernel (top-right corner of the notebook). It should be the same environment where you installed pandas, matplotlib, seaborn, and altair. If you see import errors, double-check your kernel selection.
25+
```
2026

2127
Each notebook is self-contained and includes all necessary imports and data loading, so you can start with any chapter that interests you.
2228

docs/src/vscode_setup.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,43 @@ This creates a new `.ipynb` notebook file where you can add cells and run them i
196196
- Cell execution controls
197197
- Markdown and code cells
198198

199+
## Selecting the Right Python Environment
200+
201+
After installing packages, it's crucial to ensure VS Code uses the correct Python environment. This is especially important if you have multiple Python installations.
202+
203+
### For Python Files (.py)
204+
205+
When working with Python files, VS Code needs to know which Python interpreter to use:
206+
207+
1. **Automatic Selection**: VS Code usually detects your Python installation automatically
208+
2. **Manual Selection**: If needed, press `Ctrl+Shift+P` (or `Cmd+Shift+P`) and type "Python: Select Interpreter"
209+
3. **Choose Your Environment**: Select the Python installation that has your packages installed:
210+
- If using uv: Look for the interpreter in your project's `.venv` folder
211+
- If using Anaconda: Choose the anaconda or miniconda interpreter
212+
- If using system Python: Choose the global Python installation
213+
214+
4. **Verify Selection**: Check the bottom-left status bar - it should show your selected Python version
215+
216+
### For Jupyter Notebooks (.ipynb)
217+
218+
Notebooks use "kernels" (similar to interpreters) to run code:
219+
220+
1. **Initial Kernel Selection**: When you create or open a notebook, VS Code may prompt you to select a kernel
221+
2. **Manual Kernel Selection**: Click the kernel name in the top-right corner of the notebook to change it
222+
3. **Choose the Right Kernel**: Select the same Python environment where you installed packages
223+
4. **Verify Connection**: A green dot next to the kernel name means it's connected and ready
224+
225+
### Troubleshooting Environment Issues
226+
227+
If your imports don't work or packages aren't found:
228+
229+
1. **Check Active Environment**: Verify VS Code is using the environment where you installed packages
230+
2. **Restart Kernel**: In notebooks, use "Restart Kernel" from the kernel menu
231+
3. **Refresh Interpreters**: Use Command Palette → "Python: Refresh" to update the interpreter list
232+
4. **Reinstall Packages**: If still having issues, reinstall packages in the correct environment
233+
234+
```{tip}
235+
**Pro tip**: The status bar (bottom of VS Code) always shows your active Python interpreter. Make sure it matches the environment where you installed your packages!
236+
```
237+
199238
Now you're ready to start learning Python for data analysis! The next chapter will introduce you to working with Jupyter notebooks in VS Code.

0 commit comments

Comments
 (0)