[Python] execute the code correctly with .py, but Jupyter Notebook reports an error

Discussão em 'Python' iniciado por Stack, Julho 24, 2025.

  1. Stack

    Stack Membro Participativo

    import torchvision
    trian_set = torchvision.datasets.CIFAR10(root="./mydata", train= True, download= True)
    test_set = torchvision.datasets.CIFAR10(root="./mydata", train= False, download= True)


    In the same virtual environment, I can execute the above code correctly with .py, but Jupyter Notebook reports an error. How can I solve this?


    ImportError: DLL load failed while importing _imaging: 操作系统无法运行 %1。
    ​

    pillow version already matched pytorch version

    import os
    import sys

    # 添加 Python DLL 和 site-packages 目录(Pillow 的 _imaging.pyd 依赖 DLL)
    python_path = sys.exec_prefix
    os.environ["PATH"] = (
    python_path + r"\Library\bin;" +
    python_path + r"\Library\lib;" +
    python_path + r"\Lib\site-packages\PIL;" +
    os.environ["PATH"]
    )

    # 再导入 Pillow
    from PIL import Image
    print("Pillow version:", Image.__version__)


    sometimes I can fix the problem by import OS, but it does not work all the time.

    ImportError: DLL load failed while importing _imaging: 操作系统无法运行 %1。How can I solve the problem completely?

    Continue reading...

Compartilhe esta Página