Running python notebook in vscode using a pdm environment

See original GitHub issue

First, thanks for providing us with pdm and good documentation, great work!

  • I have searched the issue tracker and believe that this is not a duplicate.

It seems we cannot run a jupyter notebook file using the python/notebook extension inside vscode. As vscode is asking for the python interpreter to use, and selecting the main python interpreter does not seem to load the pdm environment. we get the error: Running cells with 'Python 3.7.5 64-bit' requires ipykernel package

I am not sure it is a bug as such but could not find any mention in the documentation. Is there a workaround to make the notebook extension recognize the correct environment? Or is it a limitation of using pep582 and no venv?

(as a side note, running pdm run jupyter notebook from terminal allow to run the notebook in the standard jupyter notebook web-based page, but this is not what I need, I need work on the notebook file from vscode directly)

Steps to reproduce

  • Create a new project with pdm pep582 gloabaly and use global cache.
  • Add the correct param in .vscode/settings.json so that we can run python script straight from vscode (working). pdm add jupyterlab
  • Add a jupyter notebook file that just ouput the version of pandas for example. in vscode, install the notebook extension and try to open the notebook file inside vscode.

Actual behavior

Error in vscode notebook viewer, once the interpreter has been selected, it does not see the pdm environment and therefore no ipykernel.

Expected behavior

A method to specify to vscode notebook extension how to “see” the pdm environment.

Environment Information

pdm info:
PDM version:        1.12.1
Python Interpreter: C:/Users/Me/AppData/Local/Programs/Python/Python37/python.EXE (3.7)
Project Root:       C:/Users/Me/Devel/test/jup
Project Packages:   C:\Users\Me\Devel\test\jup\__pypackages__\3.7

pdm info --env:
{
  "implementation_name": "cpython",
  "implementation_version": "3.7.5",
  "os_name": "nt",
  "platform_machine": "AMD64",
  "platform_release": "10",
  "platform_system": "Windows",
  "platform_version": "10.0.19041",
  "python_full_version": "3.7.5",
  "platform_python_implementation": "CPython",
  "python_version": "3.7",
  "sys_platform": "win32"
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
bx2commented, Feb 8, 2022

There is a simpler way to do it. Add a file called .env inside of your project directory and set your paths there, like so:

PYTHONPATH=/your-workspace-path/__pypackages__/<major>.<minor>/lib

jupyter notebook reads .env files and sets everything that is needed.

3reactions
hcoohbcommented, Jan 20, 2022

After a bit of investigation, I have found a reliable way of using vscode notebook with a pdm environment (PEP582 global). I thought I would share as it may help others (and maybe we can add details to the docs?).

So, basically you will just need to create a new ipykernel spec that can be read by VSCODE.

  • Create a folder called PDM_env inside the folder for ipyernel specs for your system (listed here - on windows it is %APPDATA%\jupyter\kernels)
  • Inside create a file called kernel.json with the following:
 "argv": [
  "C:\\Users\\USERNAME\\AppData\\Local\\Programs\\Python\\Python37\\python.exe",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "PDM_env",
 "language": "python",
 "env": {"PYTHONPATH": "C:\\Users\\USERNAME\\AppData\\Roaming\\pdm\\venv\\lib\\site-packages\\pdm\\pep582"}
}
  • Change the python binary path, as well as the path to your global pdm pep582 folder
  • Restart vscode and you can now select the kernel “PDM_env” that will load the ipykernel you installed inside the current pdm environment.

This means you only have to create this file once, and then just launch vscode from the folder of the pdm environment.

Another great possibility is to create a virtual env that only contains the ipykernel, that way no need to install it inside the pdm environment we wish to run. we just need to change the kernel.spec to point the python binary path to the one inside our virtual environment. (Although that means we are mixing 2 environments, so package compatibility/versioning might no longer be correct…)

Hope my explanations are clear enough…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with Jupyter Notebooks in Visual Studio Code
To select an environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P).
Read more >
Working with PEP 582 - PDM
This file can be auto-generated with plugin pdm-vscode . Enable PEP582 globally, and make sure VSCode runs using the same user and shell...
Read more >
running cells with 'python 3.10.4 64-bit' requires ipykernel ...
Error in vscode notebook viewer, once the interpreter has been selected, it does not see the pdm environment and therefore no ipykernel. Expected...
Read more >
could not link test program to python
Local computer: start the VS Code debugger using the modified Python: Attach ... Environment from your Terminal/Command Window and then launch VS Code....
Read more >
How To Setup Jupyter Notebook in VS Code (w/ Virtual Env ...
Figuring out how to how setup a jupyter notebook is pretty easy – you just open your terminal, install jupyter & run jupyter...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found