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

如何设置环境变量`JUPYTER_PLATFORM_DIRS=1`并解决Colab运行报错

解决Google Colab中Jupyter路径迁移的DeprecationWarning问题

你在Google Colab运行sculpt_faces.py时偶尔遇到的DeprecationWarning,是由于jupyter-core正迁移到基于platformdirs库的标准路径机制导致的。以下是具体解决步骤:

问题复现

运行脚本时出现的报错:

Traceback (most recent call last):
  File "sculpt_faces.py", line 25, in <module>
    from matplotlib.pyplot import cm
  File "/usr/local/lib/python3.8/dist-packages/matplotlib/pyplot.py", line 2282, in <module>
    switch_backend(rcParams["backend"])
  File "/usr/local/lib/python3.8/dist-packages/matplotlib/pyplot.py", line 221, in switch_backend
    backend_mod = importlib.import_module(backend_name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/local/lib/python3.8/dist-packages/ipykernel/__init__.py", line 2, in <module>
    from .connect import *
  File "/usr/local/lib/python3.8/dist-packages/ipykernel/connect.py", line 18, in <module>
    import jupyter_client
  File "/usr/local/lib/python3.8/dist-packages/jupyter_client/__init__.py", line 4, in <module>
    from .connect import *
  File "/usr/local/lib/python3.8/dist-packages/jupyter_client/connect.py", line 28, in <module>
    from jupyter_core.paths import jupyter_data_dir, jupyter_runtime_dir, secure_write
  File "/usr/local/lib/python3.8/dist-packages/jupyter_core/paths.py", line 176, in <module>
    deprecation(
  File "/usr/local/lib/python3.8/dist-packages/jupyter_core/utils/__init__.py", line 87, in deprecation
    warnings.warn(message, DeprecationWarning, stacklevel=stacklevel + 1)
DeprecationWarning: Jupyter is migrating its paths to use standard platformdirs
given by the platformdirs library.  To remove this warning and
see the appropriate new directories, set the environment variable
`JUPYTER_PLATFORM_DIRS=1` and then run `jupyter --paths`.
The use of platformdirs will be the default in `jupyter_core` v6

已确认环境依赖满足:

Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Requirement already satisfied: jupyter-core in /usr/local/lib/python3.8/dist-packages (5.1.0)
Requirement already satisfied: traitlets>=5.3 in /usr/local/lib/python3.8/dist-packages (from jupyter-core) (5.6.0)
Requirement already satisfied: platformdirs>=2.5 in /usr/local/lib/python3.8/dist-packages (from jupyter-core) (2.5.4)

解决方法

你只需要设置环境变量JUPYTER_PLATFORM_DIRS=1,有三种可行方式:

方式1:Colab单元格前置设置

在运行脚本的单元格前,先执行环境变量配置:

%env JUPYTER_PLATFORM_DIRS=1
!python sculpt_faces.py

方式2:修改脚本开头

在sculpt_faces.py中,所有import语句之前添加以下代码:

import os
os.environ['JUPYTER_PLATFORM_DIRS'] = '1'

方式3:shell命令前缀设置

直接在运行脚本的命令中附带环境变量:

!JUPYTER_PLATFORM_DIRS=1 python sculpt_faces.py

以上任意一种方式都能启用新的路径机制,消除该DeprecationWarning。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 09:45:45