Using uv with JupyterJupyterでのuvの使用
The Jupyter notebook is a popular tool for interactive computing, data analysis, and visualization. You can use Jupyter with uv in a few different ways, either to interact with a project, or as a standalone tool.Jupyterノートブックは、インタラクティブなコンピューティング、データ分析、可視化のための人気のあるツールです。プロジェクトと対話するため、またはスタンドアロンツールとして、いくつかの異なる方法でJupyterをuvと一緒に使用できます。
Using Jupyter within a projectプロジェクト内でのJupyterの使用
If you're working within a project, you can start a Jupyter server with access to the project's virtual environment via the following:プロジェクト内で作業している場合、次のコマンドを使用してプロジェクトの仮想環境にアクセスできるJupyterサーバーを起動できます。
By default, jupyter lab will start the server at
http://localhost:8888/lab.デフォルトでは、jupyter labはサーバーをhttp://localhost:8888/labで起動します。
Within a notebook, you can import your project's modules as you would in any other file in the
project. For example, if your project depends on requests, import requests will import
requests from the project's virtual environment.ノートブック内では、プロジェクト内の他のファイルと同様に、プロジェクトのモジュールをインポートできます。たとえば、プロジェクトがrequestsに依存している場合、import requestsはプロジェクトの仮想環境からrequestsをインポートします。
If you're looking for read-only access to the project's virtual environment, then there's nothing more to it. However, if you need to install additional packages from within the notebook, there are a few extra details to consider.プロジェクトの仮想環境への読み取り専用アクセスを探している場合、特に何もする必要はありません。ただし、ノートブック内から追加のパッケージをインストールする必要がある場合は、考慮すべきいくつかの追加の詳細があります。
Creating a kernelカーネルの作成
If you need to install packages from within the notebook, we recommend creating a dedicated kernel for your project. Kernels enable the Jupyter server to run in one environment, with individual notebooks running in their own, separate environments.ノートブック内からパッケージをインストールする必要がある場合、プロジェクト専用のカーネルを作成することをお勧めします。カーネルは、Jupyterサーバーが1つの環境で実行され、個々のノートブックがそれぞれの独立した環境で実行されることを可能にします。
In the context of uv, we can create a kernel for a project while installing Jupyter itself in an
isolated environment, as in uv run --with jupyter jupyter lab. Creating a kernel for the project
ensures that the notebook is hooked up to the correct environment, and that any packages installed
from within the notebook are installed into the project's virtual environment.uvの文脈では、uv run --with jupyter jupyter labのように、Jupyter自体を隔離された環境にインストールしながらプロジェクトのカーネルを作成できます。プロジェクトのカーネルを作成することで、ノートブックが正しい環境に接続され、ノートブック内からインストールされたパッケージがプロジェクトの仮想環境にインストールされることが保証されます。
To create a kernel, you'll need to install ipykernel as a development dependency:カーネルを作成するには、ipykernelを開発依存関係としてインストールする必要があります。
Then, you can create the kernel for project with:次に、projectのカーネルを作成するには、次のコマンドを実行します:
From there, start the server with:そこから、サーバーを起動するには、次のコマンドを実行します:
When creating a notebook, select the project kernel from the dropdown. Then use !uv add pydantic
to add pydantic to the project's dependencies, or !uv pip install pydantic to install pydantic
into the project's virtual environment without persisting the change to the project pyproject.toml
or uv.lock files. Either command will make import pydantic work within the notebook.ノートブックを作成する際には、ドロップダウンからprojectカーネルを選択します。その後、!uv add pydanticを使用してpydanticをプロジェクトの依存関係に追加するか、!uv pip install pydanticを使用してプロジェクトの仮想環境にpydanticをインストールし、変更をプロジェクトのpyproject.tomlやuv.lockファイルに永続化しないようにします。どちらのコマンドでも、ノートブック内でimport pydanticが機能するようになります。
Installing packages without a kernelカーネルなしでパッケージをインストールする
If you don't want to create a kernel, you can still install packages from within the notebook. However, there are a few caveats to consider.カーネルを作成したくない場合でも、ノートブック内からパッケージをインストールすることは可能です。 ただし、考慮すべきいくつかの注意点があります。
Though uv run --with jupyter runs in an isolated environment, within the notebook itself,
!uv add and related commands will modify the project's environment, even without a kernel.uv run --with jupyterは隔離された環境で実行されますが、ノートブック内では、
!uv addや関連コマンドはカーネルなしでもプロジェクトの環境を変更します。
For example, running !uv add pydantic from within a notebook will add pydantic to the project's
dependencies and virtual environment, such that import pydantic will work immediately, without
further configuration or a server restart.例えば、ノートブック内から!uv add pydanticを実行すると、pydanticがプロジェクトの依存関係と仮想環境に追加され、import pydanticがすぐに機能するようになります。追加の設定やサーバーの再起動は不要です。
However, since the Jupyter server is the "active" environment, !uv pip install will install
package's into Jupyter's environment, not the project environment. Such dependencies will persist
for the lifetime of the Jupyter server, but may disappear on subsequent jupyter invocations.ただし、Jupyterサーバーが「アクティブ」な環境であるため、!uv pip installはパッケージをJupyterの環境にインストールします。これらの依存関係はJupyterサーバーのライフタイム中は持続しますが、次回のjupyterの呼び出し時には消える可能性があります。
If you're working with a notebook that relies on pip (e.g., via the %pip magic), you can include
pip in your project's virtual environment by running uv venv --seed prior to starting the Jupyter
server. For example, given:もし、%pipマジックを介してpipに依存するノートブックで作業している場合は、Jupyterサーバーを起動する前にuv venv --seedを実行することで、pipをプロジェクトの仮想環境に含めることができます。例えば、次のように:
Subsequent %pip install invocations within the notebook will install packages into the project's
virtual environment. However, such modifications will not be reflected in the project's
pyproject.toml or uv.lock files.その後の %pip install の呼び出しは、プロジェクトの仮想環境にパッケージをインストールします。しかし、そのような変更はプロジェクトの pyproject.toml や uv.lock ファイルには 反映されません。
Using Jupyter as a standalone toolJupyterをスタンドアロンツールとして使用する
If you ever need ad hoc access to a notebook (i.e., to run a Python snippet interactively), you can
start a Jupyter server at any time with uv tool run jupyter lab. This will run a Jupyter server in
an isolated environment.ノートブックに対してアドホックアクセスが必要な場合(つまり、Pythonスニペットをインタラクティブに実行するため)、uv tool run jupyter lab を使用していつでもJupyterサーバーを起動できます。これにより、隔離された環境でJupyterサーバーが実行されます。
Using Jupyter with a non-project environmentプロジェクト環境以外でJupyterを使用する
If you need to run Jupyter in a virtual environment that isn't associated with a
project (e.g., has no pyproject.toml or uv.lock), you can do
so by adding Jupyter to the environment directly. For example:プロジェクトに関連付けられていない仮想環境でJupyterを実行する必要がある場合(例えば、pyproject.toml や uv.lock がない場合)、Jupyterを環境に直接追加することで実行できます。例えば:
From here, import pydantic will work within the notebook, and you can install additional packages
via !uv pip install, or even !pip install.ここから、import pydantic はノートブック内で動作し、!uv pip install または !pip install を使用して追加のパッケージをインストールできます。
Using Jupyter from VS CodeVS CodeからJupyterを使用する
You can also engage with Jupyter notebooks from within an editor like VS Code. To connect a uv-managed project to a Jupyter notebook within VS Code, we recommend creating a kernel for the project, as in the following:VS CodeのようなエディタからJupyterノートブックにアクセスすることもできます。uv管理プロジェクトをVS Code内のJupyterノートブックに接続するには、次のようにプロジェクトのカーネルを作成することをお勧めします:
# Create a project.
$ uv init project
# Move into the project directory.
$ cd project
# Add ipykernel as a dev dependency.
$ uv add --dev ipykernel
# Open the project in VS Code.
$ code .
Once the project directory is open in VS Code, you can create a new Jupyter notebook by selecting
"Create: New Jupyter Notebook" from the command palette. When prompted to select a kernel, choose
"Python Environments" and select the virtual environment you created earlier (e.g.,
.venv/bin/python on macOS and Linux, or .venv\Scripts\python on Windows).プロジェクトディレクトリがVS Codeで開かれたら、コマンドパレットから「Create: New Jupyter Notebook」を選択して新しいJupyterノートブックを作成できます。カーネルを選択するように求められたら、「Python Environments」を選択し、以前に作成した仮想環境(例:macOSおよびLinuxでは .venv/bin/python、Windowsでは .venv\Scripts\python)を選択します。
Note注意
VS Code requires ipykernel to be present in the project environment. If you'd prefer to avoid
adding ipykernel as a dev dependency, you can install it directly into the project environment
with uv pip install ipykernel.VS Codeでは、プロジェクト環境に ipykernel が存在する必要があります。ipykernel を開発依存関係として追加したくない場合は、uv pip install ipykernel を使用してプロジェクト環境に直接インストールできます。
If you need to manipulate the project's environment from within the notebook, you may need to add
uv as an explicit development dependency:ノートブック内からプロジェクトの環境を操作する必要がある場合は、uvを明示的な開発依存関係として追加する必要があります。
From there, you can use !uv add pydantic to add pydantic to the project's dependencies, or
!uv pip install pydantic to install pydantic into the project's virtual environment without
updating the project's pyproject.toml or uv.lock files.そこから、!uv add pydanticを使用してpydanticをプロジェクトの依存関係に追加するか、!uv pip install pydanticを使用してプロジェクトの仮想環境にpydanticをインストールし、プロジェクトのpyproject.tomlやuv.lockファイルを更新しないことができます。