Use Jupyter Notebook with Visual Studio Code on Windows 10 + Python + Poetry + pyenv-win

Introduction

I found a procedure to use Jupyter Notebook in Visual Studio Code and a procedure to use Jupyter Notebook in Mac, but I could not find a procedure in the environment that combines Windows 10 with Poetry and pyenv-win, so in this environment I summarized the procedure to use Jupyter Notebook from Visual Studio Code.

With Anaconda, you can easily use Jupyter Notebook, but one reason is that you haven't used Anaconda these days for religious reasons.

environment

Premise

Python 3.8.5 + pyenv-win + Poetry is installed by the following procedure. Using Poetry and pyenv from Python installation on Windows 10 (https://qiita.com/kerobot/items/3f4064d5174676080585)

I have installed "Python" as an extension in Visual Studio Code. Python Extensions in Visual Studio Code

Check pyenv-win and global settings for Python

Use the Powershell gcm command to find the location of pyenv-win.

> gcm pyenv | fl

Check the version of pyenv-win.

> pyenv --version
pyenv 2.64.2

Use pyenv-win to set up Python in the global environment.

> pyenv versions
> pyenv install 3.8.5
> pyenv global 3.8.5
> pyenv rehash
> python -V
Python 3.8.5

Upgrade the configured Python pip.

> python -m pip install --upgrade pip
> pip -V
pip 20.3.3

Poetry verification, project creation and Python local settings

Use Powershell's gcm command to find the location of Poetry.

> gcm poetry | fl

Check the version of Poetry.

> poetry --version
Poetry version 1.1.0

Create a project (project directory) using Poetry.

> poetry new jupyter

Go to the project you created.

> cd jupyter

Use pyenv-win to set up Python for your project's local environment.

> pyenv local 3.8.5
> pyenv rehash
> Python -V
Python 3.8.5

Build a Python virtual environment using Poetry.

> poetry install

Creating virtualenv jupyter in D:\Develop\Git\jupyter\.venv
Updating dependencies
Resolving dependencies...

Writing lock file

Package operations: 10 installs, 0 updates, 0 removals

  • Installing pyparsing (2.4.7)
  • Installing atomicwrites (1.4.0)
  • Installing attrs (20.3.0)
  • Installing colorama (0.4.4)
  • Installing more-itertools (8.6.0)
  • Installing packaging (20.8)
  • Installing pluggy (0.13.1)
  • Installing py (1.10.0)
  • Installing wcwidth (0.2.5)
  • Installing pytest (5.4.3)

Installing the current project: jupyter (0.1.0)

Add the following packages.

> poetry add pylint
> poetry add numpy==1.19.3
> poetry add pandas
> poetry add ipykernel

For Windows 10 2004 (20H2), there is a problem with fmod as of December 2020, and when I run numpy 1.19.4 using it, I get an error. I am using 1.19.3 to avoid errors. fmod(), after an update to windows 2004, is causing a strange interaction with other code

Use Poetry to check the operation.

> poetry run python -V
Python 3.8.5

Introducing Jupyter Notebook

Install jupyter using pip instead of Poetry when the Python virtual environment is not enabled.

> pip install jupyter

When I installed it using Poetry, I could not install it due to the following assertion error, so I installed it using pip.

D:\Develop\Git\jupyter> poetry add jupyter
Using version ^1.0.0 for jupyter

Updating dependencies
Resolving dependencies...

  AssertionError

  at ~\.poetry\lib\poetry\mixology\incompatibility.py:111 in __str__
      107│         )
      108│
      109│     def __str__(self):
      110│         if isinstance(self._cause, DependencyCause):
    → 111│             assert len(self._terms) == 2
      112│
      113│             depender = self._terms[0]
      114│             dependee = self._terms[1]
      115│             assert depender.is_positive()

You will get a character code and PATH warning, but leave it as it is.

WARNING: Subprocess output does not appear to be encoded as cp932
...
WARNING: The scripts jupyter-migrate.exe, jupyter-troubleshoot.exe and jupyter.exe are installed in 'c:\users\user\.pyenv\pyenv-win\versions\3.8.5\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The script jsonschema.exe is installed in 'c:\users\user\.pyenv\pyenv-win\versions\3.8.5\Scripts' which is not on PATH.
...

I think the character code warning is due to the change in the character code of pyenv-win from CP1250 to CP65001.

Choosing a Python interpreter for Visual Studio Code

Launch Visual Studio Code in your project's directory.

> code .

Select the Python interpreter that Visual Studio Code will use.

Open the command palette with Ctrl + Shift + P, type python select interpreter, and select ".venv \ Scripts \ python.exe" as the Python interpreter environment. img01.jpg

Make sure that settings.json is created in your project's .vscode directory and has the following content:

{
    "python.pythonPath": ".venv\\Scripts\\python.exe"
}

Creating a notebook from Visual Studio Code

Create a notebook for use with Visual Studio Code.

Open the command palette with Ctrl + Shift + P, type jupyter create new, and select Jupyter: Create New Blank Jupyter Notebook to create a new notebook. img02.jpg

The Python virtual environment will automatically start and you will be able to use your notebook. img03.jpg

Using notebooks from Visual Studio Code

You can now run your Python script. img04.jpg

You can press Ctrl + Enter to execute a cell and Ctrl + Shift + Enter to insert a cell.

in conclusion

I stumbled upon the introduction of jupyter, but I was able to use Jupyter Notebook with Visual Studio Code on Windows 10.

It's nice to be able to edit and save notebooks with a familiar editor. Since it uses pyenv-win and Poetry, it seems easy to try the required version and package combination.

Recommended Posts

Use Jupyter Notebook with Visual Studio Code on Windows 10 + Python + Poetry + pyenv-win
Python development environment with Windows + Anaconda3 + Visual Studio Code
Python development environment with Windows + Python + PipEnv + Visual Studio Code
Install python and Visual Studio Code on windows10 (April 2020 version)
Build Python3 for Windows 10 on ARM with Visual Studio 2019 (x86) on Windows 10 on ARM
Build jupyter notebook environment with Visual Studio Code (VS Code) Mac version
How to build a Python virtual execution environment using Visual Studio Code and pipenv on a Windows machine (also Jupyter notebook)
Build Python development environment with Visual Studio Code
Create a Python development environment on Windows (Visual Studio Code remote WSL).
Preparing to use Tensorflow (Anaconda) with Visual Studio Code
Try debugging Python on Raspberry Pi with Visual Studio.
[Windows] [Python3] Install python3 and Jupyter Notebook (formerly ipython notebook) on Windows
Use pip with Jupyter Notebook
Use Cython with Jupyter Notebook
Use Python on Windows (PyCharm)
Run Jupyter Notebook on windows
Execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people) Debugging
Set up a Python development environment with Visual Studio Code
Execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people) Environment construction
Formatting with autopep8 on Jupyter notebook
Use markdown with jupyter notebook (with shortcut)
Use Tensorflow 2.1.0 with Anaconda on Windows 10!
Use Windows 10 speech synthesis with Python
Easy to use Jupyter notebook (Python3.5)
Getting started with Python 3.8 on Windows
Try to create a python environment with Visual Studio Code & WSL
Bash, Python, Javascript, code command, etc. in Visual Studio Code on Mac
Edit files directly on Linux with Visual Studio Code Remote SSH
Installation of Visual studio code and installation of python
Use nb extensions with Anaconda's Jupyter notebook
Run servo with Python on ESP32 (Windows)
Use apache Spark with jupyter notebook (IPython notebook)
Use Jupyter Lab and Jupyter Notebook with EC2
Try SVM with scikit-learn on Jupyter Notebook
Use without installing python 2.x on Windows
How to use jupyter notebook with ABCI
Linking python and JavaScript with jupyter notebook
Use vim keybindings on Docker-launched Jupyter Notebook
Run Python in C ++ on Visual Studio 2017
Steps to create a Python virtual environment with VS Code on Windows
From re-environment construction of Python to graph drawing (on visual studio code)
Build an environment to execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people)
Python on Windows
Run Python YOLOv3 in C ++ on Visual Studio 2017
Python (Windows 10) Virtual Environment / Package with VS Code
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Enable Jupyter Notebook with conda on remote server
Django with Python Tools 2.2 for Visual Studio (PTVS 2.2)
I customized it with Visual Studio Code (mainly for python), so I will summarize it
Use Python in Anaconda environment with VS Code
Until you use PhantomJS with Python on Heroku
Settings for Python coding in Visual Studio Code
A note I was addicted to when running Python with Visual Studio Code
Use Python / Django with Windows Azure Cloud Service!
[Visual Studio Code] [Python] Tasks.json + problemMatcher settings for Python
Run Tensorflow from Jupyter Notebook on Bash on Ubuntu on Windows
Problems with windows python being called on pipenv on WSL
Monitor the training model with TensorBord on Jupyter Notebook
EC2 provisioning with Vagrant + Jupyter (IPython Notebook) on Docker
PIL with Python on Windows 8 (for Google App Engine)
Make Visual Studio Code autocomplete for python external libraries