如何为Datalab中所有Notebook配置类似Jupyter的启动脚本?
Great question! Yes, Datalab does offer a similar functionality to Jupyter's ~/.ipython/profile_default/startup folder for running Python code automatically when a Notebook kernel starts—and it's distinct from the /content/datalab/.config/startup.sh script you mentioned. Here's how it works:
How to set up auto-run Python scripts for Notebook kernels
Global scope (all Notebooks)
Place your.pystartup scripts in the/usr/local/share/datalab/startup/directory. Any Python files here will execute automatically every time a new Notebook kernel initializes. This is perfect for common tasks like importing frequently used modules (from mymodels import *) or configuring database connections (my_db.configure(production.ini)).Project-specific scope
If you only want scripts to run for Notebooks in a specific project, create adatalab/startup/subfolder within your project's root directory. Drop your startup.pyfiles here, and only Notebooks in that project will load them on kernel startup.
Key difference from /content/datalab/.config/startup.sh
As you noted, startup.sh runs before the Notebook daemon starts—it's meant for system-level tasks like installing dependencies, setting environment variables, or modifying system configurations. The Python startup scripts we discussed run when the kernel starts, just like Jupyter's startup folder, focusing specifically on Python runtime setup.
Quick way to test
Create a simple test script, e.g., /usr/local/share/datalab/startup/test_init.py, with:
print("Datalab startup script executed!") my_global_var = "Available in all notebooks"
When you launch a new Notebook, you'll see the print message in the kernel logs, and my_global_var will be immediately available in your Notebook cells without manual import.
内容的提问来源于stack exchange,提问作者Charles V.

