You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google Cloud Platform上运行已创建的Jupyter Notebook

Got it, let's sort this out for you—you don't have to copy-paste your Jupyter Notebooks from your GCP Storage Bucket to your VM instance. Here are three straightforward ways to access and use those notebooks directly:

1. Mount your Storage Bucket to the VM's file system (most intuitive)

This method makes your bucket's files appear directly in Jupyter's directory tree, just like local files. Here's how to set it up:

  • First, install gcsfuse (GCP's tool for mounting Cloud Storage buckets as file systems) in your VM's terminal:
    sudo apt-get update
    sudo apt-get install gcsfuse
    
  • Create a local directory to serve as the mount point (we'll use the Jupyter home directory for easy access):
    mkdir ~/my-bucket-mount
    
  • Mount your bucket to this directory:
    gcsfuse name-of-bucket ~/my-bucket-mount
    

Once mounted, refresh Jupyter's directory tree—you'll see the my-bucket-mount folder, and inside it, all the files from your storage bucket (including your notebooks). You can open, edit, and run them just like any local notebook file.

Pro tip for persistent mounting

If you want the bucket to stay mounted after restarting your VM, add an entry to /etc/fstab:

  1. Open the file with sudo:
    sudo nano /etc/fstab
    
  2. Add this line at the end (replace placeholders with your bucket name and mount path):
    name-of-bucket /home/jupyter/my-bucket-mount gcsfuse rw,user,allow_other
    
  3. Save and exit (Ctrl+O, then Ctrl+X in nano). Next time your VM starts, the bucket will mount automatically.

2. Load and run notebooks directly from the bucket using code

If you don't want to mount the bucket, you can programmatically load a notebook from the bucket, run its contents, or save it to your local instance. Here's how:

  • First, install nbformat (a library for working with Jupyter notebook files) if you don't have it:
    pip install nbformat
    
  • Use this code to fetch, parse, and either run or save your notebook:
    from google.cloud import storage
    import nbformat
    from nbconvert.preprocessors import ExecutePreprocessor
    
    # Initialize the storage client
    client = storage.Client()
    bucket = client.get_bucket('name-of-bucket')
    
    # Fetch the notebook file from the bucket
    notebook_blob = bucket.blob('path/to/your/notebook.ipynb')
    notebook_content = notebook_blob.download_as_string().decode('utf-8')
    
    # Parse the notebook content
    nb = nbformat.reads(notebook_content, as_version=4)
    
    # Option 1: Run the notebook's code cells
    ep = ExecutePreprocessor(timeout=600, kernel_name='python3')
    ep.preprocess(nb, {'metadata': {'path': './'}})
    
    # Option 2: Save the notebook to your local instance (so it appears in Jupyter's directory)
    with open('./loaded_notebook.ipynb', 'w') as f:
        nbformat.write(nb, f)
    

3. Use Managed Notebooks' built-in Cloud Storage integration (if applicable)

If you're using GCP's Managed Notebooks (not a plain VM with Jupyter installed manually), there's a built-in way to connect your bucket without extra tools:

  • In Jupyter's left sidebar, click the Files icon
  • Click Add folder > Cloud Storage
  • Select your storage bucket from the list
    Your bucket will appear directly in the directory tree, and you can access all your notebooks instantly.

Important note about permissions

Make sure your VM/Notebook instance's service account has the right permissions to access the bucket. At minimum, assign the Storage Object Viewer role (for read access) or Storage Admin (if you need to edit/save files back to the bucket). You can set this in the GCP Console under IAM & Admin > IAM.

内容的提问来源于stack exchange,提问作者Chase Brumfield

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:59:39