nteract/papermill

kernel_name is set, but `which python` and `which pip` point to the wrong paths

开放

#625 创建于 2021年8月2日

 (0 条评论) (0 个反应) (0 位负责人)Python (402 个派生)batch import
bughelp wanted

仓库指标

星标
 (5,381 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

🐛 Bug

I'm not sure if this is expected or not. Our use case at Google is that we are testing multiple notebooks by executing them. We dynamically create kernels and create environments for each. This is required to test the notebooks independently.

    # Create environment
    kernel_name = str(uuid.uuid4())
    env_name = f"{ENVIRONMENTS_PATH}/{kernel_name}"
    virtualenv.cli_run([env_name, "--system-site-packages"])

    # Create kernel spec
    kernel_spec = {
        "argv": [
            f"{env_name}/bin/python",
            "-m",
            "ipykernel_launcher",
            "-f",
            "{connection_file}",
        ],
        "display_name": "Python 3",
        "language": "python",
    }
    kernel_spec_folder = os.path.join(KERNELS_SPECS_PATH, kernel_name)
    kernel_spec_file = os.path.join(kernel_spec_folder, "kernel.json")

    # Create kernel spec folder
    if not os.path.exists(os.path.dirname(kernel_spec_file)):
        try:
            os.makedirs(os.path.dirname(kernel_spec_file))
        except OSError as exc:  # Guard against race condition
            if exc.errno != errno.EEXIST:
                raise

    with open(kernel_spec_file, mode="w", encoding="utf-8") as f:
        json.dump(kernel_spec, f)

    # Install kernel
    kernel_spec_manager = KernelSpecManager()
    kernel_spec_manager.install_kernel_spec(
        source_dir=kernel_spec_folder, kernel_name=kernel_name
    )

Then we run the kernel using papermill by passing the kernel_name var to the Papermill.execute_notebook method.

I created a test notebook with a cell that has ! which pip and ! which python. I expected it to point to the pip and python in the environment folder that I created earlier, but it always points to the pip and python that I run the parent script (the one coordinating the tests) in.

Reference: https://github.com/GoogleCloudPlatform/ai-platform-samples/pull/439

贡献者指南