如何在Jupyter Notebook中更改字体(含Python 2.7环境场景)
Hey there! Let’s break down how to adjust fonts in Jupyter Notebook, both generally and specifically for a Python 2.7 environment.
If you’re using a standard Jupyter setup, here’s how to tweak the fonts to your liking:
Generate the config file (if you haven’t already)
Open your terminal or command prompt and run:jupyter notebook --generate-config
This creates a config file at~/.jupyter/jupyter_notebook_config.py(Linux/macOS) orC:\Users\YourUsername\.jupyter\jupyter_notebook_config.py(Windows).Edit the config file to set your fonts
Open thejupyter_notebook_config.pyfile with a text editor. Use the search function to find these lines, remove the leading#to uncomment them, and replace the placeholder with your preferred font:- Interface-wide font:
c.NotebookApp.font_family = 'Consolas'(replace 'Consolas' with any font installed on your system) - Code cell font:
c.CodeCell.font_family = 'Monaco' - Markdown cell font:
c.MarkdownCell.font_family = 'Arial' - Want to adjust size too? Add or uncomment lines like
c.CodeCell.font_size = 14(pick your ideal size).
Pro tip: Stick to monospace fonts for code cells to keep alignment consistent – popular choices include Courier New, Fira Code, or JetBrains Mono.
- Interface-wide font:
Restart Jupyter Notebook
Close any running Jupyter instances, then launch it again from the terminal. Your new font settings should be active.
The core steps are similar, but you need to make sure you’re targeting the Python 2.7 Jupyter installation specifically:
Activate your Python 2.7 environment
If you’re using a virtual environment, activate it first:- Linux/macOS:
source your_py27_env/bin/activate - Windows:
your_py27_env\Scripts\activate
- Linux/macOS:
Generate the config file within the Python 2.7 environment
Runjupyter notebook --generate-configwhile the environment is active. This ensures the config applies to your Python 2.7 Jupyter setup, not another Python version’s.Edit the config file with font settings
Most modern Jupyter versions for Python 2.7 use the same config keys as the general case. If those don’t work (e.g., you’re on an older IPython Notebook version), try these alternative keys:- Interface font:
c.NotebookApp.interface_font = 'YourFontName' - Code cell font:
c.IPythonWidget.font_family = 'YourFontName'
- Interface font:
Restart Jupyter from the Python 2.7 environment
Close any open notebooks, shut down Jupyter, then relaunch it while your Python 2.7 environment is active. The font changes should take effect.
内容的提问来源于stack exchange,提问作者Mihajlo.Gluhovic

