Skip to content

Commit 513f675

Browse files
Align python venvs (#57)
1 parent 9da1158 commit 513f675

File tree

4 files changed

+154
-8
lines changed

4 files changed

+154
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ For immediate help with common errors, diagnostic commands, and step-by-step sol
171171

172172
---
173173

174-
## 📂 Repo Structure
174+
## 📂 Repo Structure
175175

176176
### 🦅 High-level
177177

infrastructure/simple-apim/create.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@
124124
],
125125
"metadata": {
126126
"kernelspec": {
127-
"display_name": ".venv",
127+
"display_name": "APIM Samples Python 3.12",
128128
"language": "python",
129-
"name": "python3"
129+
"name": "apim-samples"
130130
},
131131
"language_info": {
132132
"codemirror_mode": {

setup/README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This will:
1414
- Generate `.env` file for Python path configuration
1515
- Register the standardized "APIM Samples Python 3.12" Jupyter kernel
1616
- Configure VS Code settings for automatic kernel selection
17+
- Enforce kernel consistency to prevent auto-changes
1718
- Set up optimal workspace configuration
1819

1920
## Individual Commands
@@ -30,6 +31,9 @@ python setup/setup_python_path.py --setup-kernel
3031
# Configure VS Code settings only
3132
python setup/setup_python_path.py --setup-vscode
3233

34+
# Force kernel consistency (fix kernel switching issues)
35+
python setup/setup_python_path.py --force-kernel
36+
3337
# Basic PYTHONPATH setup for current session
3438
python setup/setup_python_path.py --run-only
3539

@@ -42,20 +46,38 @@ python setup/setup_python_path.py
4246
After setup, verify everything is working correctly:
4347

4448
```shell
45-
python setup/verify_local_setup.py
49+
python setup/verify_setup.py
4650
```
4751

4852
This checks:
4953
- Virtual environment activation
5054
- Required package installation
5155
- Shared module imports
52-
- Jupyter kernel registration
56+
- Jupyter kernel registration with correct name/display name
5357
- VS Code settings configuration
5458
- Environment file setup
59+
- Kernel consistency enforcement
60+
61+
## Kernel Consistency
62+
63+
To ensure notebooks always use the correct kernel ("APIM Samples Python 3.12" instead of ".venv" or "python3"):
64+
65+
1. **Run the complete setup**: `python setup/setup_python_path.py --complete-setup`
66+
2. **Restart VS Code** completely
67+
3. **Verify with**: `python setup/verify_setup.py`
68+
69+
If you still see incorrect kernel names, run:
70+
```shell
71+
python setup/setup_python_path.py --force-kernel
72+
```
5573

5674
## Troubleshooting
5775

58-
If the `python` command is not found, add your virtual environment to your system's PATH:
76+
### Kernel Issues
77+
- **Problem**: Notebooks show ".venv" or "python3" instead of "APIM Samples Python 3.12"
78+
- **Solution**: Run `--force-kernel` and restart VS Code
79+
80+
### Python Command Issues
5981

6082
```shell
6183
source .venv/bin/activate # Linux/macOS

setup/setup_python_path.py

Lines changed: 126 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,120 @@ def create_vscode_settings():
270270
return True
271271

272272

273+
def validate_kernel_setup():
274+
"""
275+
Validate that the APIM Samples kernel is properly registered and accessible.
276+
277+
Returns:
278+
bool: True if kernel is properly configured, False otherwise
279+
"""
280+
281+
try:
282+
# Check if ipykernel is available
283+
result = subprocess.run([sys.executable, '-m', 'jupyter', 'kernelspec', 'list'],
284+
check=True, capture_output=True, text=True)
285+
286+
# Check if our kernel is in the list
287+
if 'apim-samples' in result.stdout:
288+
print("✅ APIM Samples kernel found in kernelspec list")
289+
return True
290+
else:
291+
print("❌ APIM Samples kernel not found in kernelspec list")
292+
return False
293+
294+
except subprocess.CalledProcessError as e:
295+
print(f"❌ Failed to check kernel list: {e}")
296+
return False
297+
except FileNotFoundError:
298+
print("❌ Jupyter not found - please ensure Jupyter is installed")
299+
return False
300+
301+
302+
def force_kernel_consistency():
303+
"""
304+
Enforce kernel consistency by removing conflicting kernels and ensuring
305+
only the APIM Samples kernel is used for notebooks.
306+
"""
307+
308+
print("🔧 Enforcing kernel consistency...")
309+
310+
# First, ensure our kernel is registered
311+
if not validate_kernel_setup():
312+
print("⚠️ Kernel not found, attempting to register...")
313+
if not install_jupyter_kernel():
314+
print("❌ Failed to register kernel - manual intervention required")
315+
return False
316+
317+
# Update VS Code settings with strict kernel enforcement
318+
project_root = get_project_root()
319+
vscode_dir = project_root / '.vscode'
320+
settings_file = vscode_dir / 'settings.json'
321+
322+
# Enhanced kernel settings that prevent VS Code from changing kernels
323+
strict_kernel_settings = {
324+
"jupyter.defaultKernel": "apim-samples",
325+
"jupyter.kernels.changeKernelIdForNotebookEnabled": False,
326+
"jupyter.kernels.filter": [
327+
{
328+
"path": "apim-samples",
329+
"type": "pythonEnvironment"
330+
}
331+
],
332+
"jupyter.preferredKernelIdForNotebook": {
333+
"*.ipynb": "apim-samples"
334+
},
335+
"jupyter.kernels.trusted": [
336+
"./.venv/Scripts/python.exe" if os.name == 'nt' else "./.venv/bin/python"
337+
],
338+
# Prevent VS Code from auto-detecting other Python environments
339+
"jupyter.kernels.excludePythonEnvironments": [
340+
"**/anaconda3/**",
341+
"**/conda/**",
342+
"**/miniconda3/**",
343+
"**/python3.*",
344+
"*/site-packages/*",
345+
"/bin/python",
346+
"/bin/python3",
347+
"/opt/python/*/bin/python*",
348+
"/usr/bin/python",
349+
"/usr/bin/python3",
350+
"/usr/local/bin/python",
351+
"/usr/local/bin/python3",
352+
"python",
353+
"python3",
354+
"**/.venv/**/python*",
355+
"**/Scripts/python*",
356+
"**/bin/python*"
357+
]
358+
}
359+
360+
try:
361+
import json
362+
363+
# Read existing settings or create new ones
364+
existing_settings = {}
365+
if settings_file.exists():
366+
try:
367+
with open(settings_file, 'r', encoding='utf-8') as f:
368+
existing_settings = json.load(f)
369+
except json.JSONDecodeError:
370+
print("⚠️ Existing settings.json has issues, creating new one")
371+
372+
# Merge settings, with our strict kernel settings taking priority
373+
existing_settings.update(strict_kernel_settings)
374+
375+
# Write updated settings
376+
with open(settings_file, 'w', encoding='utf-8') as f:
377+
json.dump(existing_settings, f, indent=4)
378+
379+
print("✅ Strict kernel enforcement settings applied")
380+
return True
381+
382+
except Exception as e:
383+
print(f"❌ Failed to update VS Code settings: {e}")
384+
return False
385+
386+
273387
def setup_complete_environment():
274388
"""
275389
Complete setup: generate .env file, register kernel, and configure VS Code.
@@ -288,25 +402,32 @@ def setup_complete_environment():
288402
print("2. Registering standardized Jupyter kernel...")
289403
kernel_success = install_jupyter_kernel()
290404

291-
# Step 3: Configure VS Code settings
405+
# Step 3: Configure VS Code settings with strict kernel enforcement
292406
print("\n3. Configuring VS Code workspace settings...")
293407
vscode_success = create_vscode_settings()
294408

409+
# Step 4: Enforce kernel consistency
410+
print("\n4. Enforcing kernel consistency for future reliability...")
411+
consistency_success = force_kernel_consistency()
412+
295413
# Summary
296414
print("\n" + "="*50)
297415
print("📋 Setup Summary:")
298416
print(f" ✅ Python path configuration: Complete")
299417
print(f" {'✅' if kernel_success else '❌'} Jupyter kernel registration: {'Complete' if kernel_success else 'Failed'}")
300418
print(f" {'✅' if vscode_success else '❌'} VS Code settings: {'Complete' if vscode_success else 'Failed'}")
419+
print(f" {'✅' if consistency_success else '❌'} Kernel consistency enforcement: {'Complete' if consistency_success else 'Failed'}")
301420

302-
if kernel_success and vscode_success:
421+
if kernel_success and vscode_success and consistency_success:
303422
print("\n🎉 Setup complete! Your local environment now matches the dev container experience.")
304423
print(" • Notebooks will automatically use the 'APIM Samples Python 3.12' kernel")
305424
print(" • Python modules from shared/ directory are available")
306425
print(" • VS Code is configured for optimal workflow")
426+
print(" • Kernel selection is locked to prevent auto-changes")
307427
print("\n💡 Next steps:")
308428
print(" 1. Restart VS Code to apply all settings")
309429
print(" 2. Open any notebook - it should automatically use the correct kernel")
430+
print(" 3. The kernel should remain consistent across all notebooks")
310431
else:
311432
print("\n⚠️ Setup completed with some issues. Check error messages above.")
312433

@@ -394,6 +515,9 @@ def show_help():
394515
elif command == "--setup-vscode":
395516
# Just configure VS Code settings
396517
create_vscode_settings()
518+
elif command == "--force-kernel":
519+
# Force kernel consistency and prevent changes
520+
force_kernel_consistency()
397521
elif command == "--complete-setup":
398522
# Full setup: everything needed for local development
399523
setup_complete_environment()

0 commit comments

Comments
 (0)