Python in VS Code on MacOS
Posted: 2024-06-18
Tags: Python, MacOS, Jupyter, VS Code
Xcode is not a good IDE for Python. It can be done1, but VS Code is a better plan.
Actionable Takeaways
Assuming Python has been installed with the Xcode commandline tools (xcode-select --install
) already.
Install Miniconda2
mkdir -p ~/miniconda3
curl https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh -o ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm -rf ~/miniconda3/miniconda.sh
~/miniconda3/bin/conda init bash
~/miniconda3/bin/conda init zsh
Create a Conda Environment3
conda create --name SomeName
To use the environment4: conda activate SomeName
When finished in the environment: conda deactivate
Download and Install VS Code
Download VS Code from Microsoft’s site.
The ZIP file only has the .app
inside. Put it in /Applications
or wherever you like.
Jupyter - Local or Global?
Installing Jupyter (pip install jupyter
) to the user’s site-packages
directory will make it available to all conda environments.
However, Jupyter brings along a ton of dependencies. To contain all of these in an environment instead of globally, activate a conda environment and use: conda install jupyter
Using Jupyter in Python5
Use Python: Select Interpreter
command from the Command Palette.
To use Jupyter in VS Code it is easiest to download the Jupyter extension in the VS Code Extension Marketplace. If not, then you must point VS Code to your running Jupyter server.
Why Miniconda?
As much as I dislike installing any extra software, it is a good idea to be able to containerize all the Python modules for easy disposal.
The main reason for Miniconda over Anaconda is its size (less than half a gigabyte) and because it includes an order of magnitude fewer random packages6. From a common sense security standpoint including less than a quarter of the packages of full Anaconda is justification enough for me.
Apple also uses Miniconda in their developer documentation7.