Running python notebook in vscode using a pdm environment
See original GitHub issueFirst, 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:
- Created 2 years ago
- Reactions:2
- Comments:11 (2 by maintainers)
Top Related StackOverflow Question
There is a simpler way to do it. Add a file called
.envinside of your project directory and set your paths there, like so:jupyter notebook reads .env files and sets everything that is needed.
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.
PDM_envinside the folder for ipyernel specs for your system (listed here - on windows it is%APPDATA%\jupyter\kernels)kernel.jsonwith the following: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…