VS Code for Neuroscience: Complete Setup Guide for macOS
VS Code for Neuroscience: Complete Setup Guide for macOS
Visual Studio Code is a powerful, free code editor that’s perfect for neuroscience research. This guide will get you up and running quickly with VS Code for Python development, PsychoPy experiments, data analysis, and computational modeling on macOS.
Quick Start: 5 Steps to Neuroscience-Ready VS Code
1. System Requirements
- macOS 10.15 or later
- 8GB RAM minimum (16GB recommended)
- Admin privileges for installation
2. The Essentials
You’ll need three main components:
- VS Code - Your code editor
- Python (via Anaconda) - Programming environment
- Key Extensions - Python support and scientific tools
Step-by-Step Installation
Install VS Code (2 minutes)
Easiest method: Direct download
- Go to code.visualstudio.com
- Click “Download for Mac”
- Drag the downloaded app to your Applications folder
- Open VS Code from Applications or Spotlight search
Alternative: If you use Homebrew
brew install --cask visual-studio-code
Install Python with Anaconda (5 minutes)
Anaconda provides Python plus scientific packages neuroscientists commonly use.
- Download Anaconda from anaconda.com
- Choose the macOS installer (Intel or Apple Silicon)
- Run the installer and follow the prompts
- Restart your terminal or run:
source ~/.zshrc
- Verify installation:
conda --version
Create Your Neuroscience Environment (3 minutes)
# Create environment with essential packages
conda create -n neuroscience python=3.11 numpy scipy pandas matplotlib jupyter -y
# Activate the environment
conda activate neuroscience
# Install neuroscience packages
pip install psychopy mne nibabel
Install Essential Extensions (2 minutes)
Extensions add functionality to VS Code. Install these must-have extensions for neuroscience:
Quick install via VS Code:
- Open VS Code
- Click the Extensions icon (puzzle piece) in the left sidebar
- Search for and install these extensions:
- Python (by Microsoft) - Python language support
- Jupyter (by Microsoft) - Notebook support
- Pylance (by Microsoft) - Enhanced Python intellisense
Command line installation:
code --install-extension ms-python.python
code --install-extension ms-toolsai.jupyter
code --install-extension ms-python.pylance
Optional but useful:
- GitLens - Enhanced Git capabilities
- Markdown All in One - Better markdown editing
- Material Icon Theme - Nice file icons
Configure Python in VS Code (1 minute)
- Open VS Code and create a new Python file (
.py
) - Press
Cmd+Shift+P
and type “Python: Select Interpreter” - Choose your conda environment:
~/anaconda3/envs/neuroscience/bin/python
That’s it! VS Code will remember this setting.
Working with PsychoPy
Installing PsychoPy
With your neuroscience environment active:
conda activate neuroscience
pip install psychopy
Running PsychoPy Experiments
- Create a new Python file (e.g.,
experiment.py
) - Write your experiment code:
from psychopy import visual, core, event
# Create window
win = visual.Window(size=(800, 600), fullscr=False)
# Create stimulus
text = visual.TextStim(win, text='Hello PsychoPy!', pos=(0, 0), height=0.1)
# Display stimulus
text.draw()
win.flip()
core.wait(2.0)
# Cleanup
win.close()
core.quit()
- Run the experiment: Press
F5
or click the “Run Python File” button
Key VS Code Features for PsychoPy
Interactive Development:
- Use
# %%
to create code cells that you can run individually - Press
Shift+Enter
to run a cell and move to the next one
Debugging:
- Set breakpoints by clicking next to line numbers
- Press
F5
to start debugging - Use the debug console to inspect variables
Code Completion:
- VS Code will suggest PsychoPy functions as you type
- Press
Ctrl+Space
to manually trigger suggestions
Data Analysis with Jupyter Notebooks
VS Code has excellent built-in support for Jupyter notebooks - perfect for neuroscience data analysis.
Working with Notebooks
- Create a new notebook:
Cmd+Shift+P
→ “Create: New Jupyter Notebook” - Select your kernel: Click “Select Kernel” → Choose “neuroscience” environment
- Start analyzing: Create code and markdown cells as needed
Key Features You’ll Love
Variable Explorer: See all your variables in the sidebar while working
- Click the “Variables” tab when running notebook cells
- Inspect DataFrames, arrays, and other objects interactively
Interactive Plots: Matplotlib and seaborn plots display inline
- No need for
plt.show()
in notebooks - Plots are saved with your notebook automatically
Code Cells in Python Files: Add # %%
to create notebook-like cells in .py
files
- Run individual cells with
Shift+Enter
- Perfect for iterative data exploration
Example: Quick EEG Analysis
# %%
import mne
import numpy as np
import matplotlib.pyplot as plt
# Load sample data
raw = mne.io.read_raw_fif('sample_data.fif', preload=True)
# %%
# Quick visualization
raw.plot_psd(fmax=50)
plt.show()
# %%
# Basic preprocessing
raw.filter(l_freq=1, h_freq=40)
raw.set_eeg_reference('average')
Getting Spyder-like Features
Many neuroscientists love Spyder’s interface. Here’s how to get similar functionality in VS Code:
Variable Explorer
- Install: Python extension (included in our setup)
- Access: Variables panel appears automatically when running code
- Features: View arrays, DataFrames, plots, and objects
Interactive Console
- Open:
View
→Terminal
→ New Terminal - Run: Type
python
to start interactive session - Use: Test code quickly without creating files
Key Shortcuts (Spyder-style)
VS Code will automatically recognize these patterns:
- F9: Run current line or selection
- Shift+Enter: Run cell and advance
- F5: Run entire file
Reference: See VS Code’s Python tutorial for more shortcuts
Computational Modeling in VS Code
VS Code excels at computational modeling thanks to its debugging capabilities and interactive features.
Getting Started with Modeling
For decision-making and learning models, you’ll mainly need:
conda activate neuroscience
pip install scipy scikit-learn
pip install pymc # For Bayesian models
pip install matplotlib seaborn # For visualization
Example: Simple Decision Model
Create a new Python file for your model:
# decision_model.py
import numpy as np
import matplotlib.pyplot as plt
def drift_diffusion_model(drift_rate, threshold, n_trials=1000):
"""Simple drift diffusion model simulation"""
decisions = []
rts = []
for trial in range(n_trials):
evidence = 0
time_steps = 0
while abs(evidence) < threshold and time_steps < 1000:
evidence += np.random.normal(drift_rate, 1)
time_steps += 1
decision = 1 if evidence > 0 else 0
rt = time_steps * 0.001 # Convert to seconds
decisions.append(decision)
rts.append(rt)
return decisions, rts
# Run simulation
decisions, rts = drift_diffusion_model(drift_rate=0.5, threshold=10)
# Plot results
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
plt.hist(rts, bins=30, alpha=0.7)
plt.xlabel('Reaction Time (s)')
plt.ylabel('Frequency')
plt.title('RT Distribution')
plt.subplot(1, 2, 2)
plt.bar([0, 1], [decisions.count(0), decisions.count(1)])
plt.xlabel('Decision')
plt.ylabel('Count')
plt.title('Decision Frequency')
plt.show()
VS Code Features for Modeling
Interactive Development: Use # %%
cells to test model components
Debugging: Set breakpoints to inspect model behavior step-by-step
Variable Inspector: Monitor arrays and results in real-time
Integrated Plots: See results immediately without switching applications
Reference: VS Code’s Data Science tutorial covers advanced modeling workflows
Quick Troubleshooting
Common Issues and Solutions
VS Code can’t find Python packages
- Check your interpreter:
Cmd+Shift+P
→ “Python: Select Interpreter” - Choose:
~/anaconda3/envs/neuroscience/bin/python
- Reload window:
Cmd+Shift+P
→ “Developer: Reload Window”
PsychoPy experiments won’t run
- Ensure your conda environment is activated:
conda activate neuroscience
- On Apple Silicon Macs, try:
arch -x86_64 python experiment.py
Jupyter notebooks won’t start
- Install ipykernel:
pip install ipykernel
- Register kernel:
python -m ipykernel install --user --name neuroscience
Slow performance with large data
- Exclude data folders from VS Code search:
- Go to Settings → Search → “files.exclude”
- Add patterns like
**/data/**
and**/*.nii.gz
Best Practices for Neuroscience Research
Project Organization:
my_experiment/
├── data/ # Raw data (never commit to git)
├── analysis/ # Analysis scripts
├── experiments/ # PsychoPy experiments
├── notebooks/ # Jupyter notebooks
└── results/ # Generated figures and results
Version Control:
- Use Git to track your code (not data)
.gitignore
large files:*.nii.gz
,*.edf
,*.h5
- Commit frequently with descriptive messages
Reproducibility:
- Include
requirements.txt
with package versions - Use random seeds in analyses:
np.random.seed(42)
- Document analysis parameters in notebooks
Learning Resources
Official Documentation:
Neuroscience-Specific:
Conclusion
You now have a powerful VS Code setup for neuroscience research! This configuration provides:
- Efficient Python development with autocomplete and debugging
- Interactive data analysis with Jupyter notebook support
- Experiment development with PsychoPy integration
- Computational modeling capabilities with scientific libraries
Start with the basics and gradually explore more advanced features as your projects grow. VS Code’s flexibility allows you to customize your environment as your research needs evolve.
Next Steps:
- Create your first experiment or analysis script
- Explore VS Code’s command palette (
Cmd+Shift+P
) - Learn keyboard shortcuts to boost productivity
- Join the VS Code community for tips and extensions
Happy coding! 🧠🔬