https://kotlinlang.org logo
Title
c

Carter

03/04/2022, 12:22 PM
I’m trying to import the kotlinx datetime library into a jupyter notebook. I thought I understand the syntax, but apparently not.
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-datetime:0.3.2")
%useLatestDescriptors
%use lets-plot

import java.util.Random
import kotlinx.datetime.Instant
Running that yields the error
Line_214.jupyter-kts (6:25 - 32) Unresolved reference: Instant
What am I missing?
a

altavir

03/04/2022, 12:27 PM
Use kotlinx-datetime-jvm. Maven resolver in the notebook can't us gradle metadata to automatically add suffixes for MPP libraries
c

Carter

03/04/2022, 12:30 PM
I did try that also but it didn’t work for me either. Specifically
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-datetime-jvm:0.3.2")
I’ve also tried creating a json descriptor and loading that.
{
  "description": "Datetime",
  "properties": {
    "v": "0.3.2"
  },
  "link": "<https://github.com/Kotlin/kotlinx-datetime>",
  "dependencies": [
    "org.jetbrains.kotlinx:kotlinx-datetime-jvm:$v"
  ],
  "imports": [
    "kotlinx.datetime.Instant"
  ]
}
Then
%use @file[/data/notebook_files/datetime.json]
Which gives effectively the same error
The problem is found in one of the loaded libraries: check library imports, dependencies and repositories
Error compiling code:
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-datetime-jvm:0.3.2")
import kotlinx.datetime.Instant


Errors:
Line_247.jupyter-kts (2:25 - 32) Unresolved reference: Instant
You can't use import in the same cell as dependency loading. I am not sure it is covered somwhere in the documentation. The problem is that cell with dependency loading uses "old" class-loader.
c

Carter

03/04/2022, 12:38 PM
I don’t think I’ve ever seen that documented.
a

altavir

03/04/2022, 12:38 PM
@Ilya Muradyan Probably it should be covered somewhere.
c

Carter

03/04/2022, 12:39 PM
I also think there was a problem with the Jupyter Kernel. I’m also using datalore and when I copied and pasted your example t to the top of my notebook it didn’t work until I restarted the kernel.
a

altavir

03/04/2022, 12:39 PM
Yes, classpath needs to be reset if it is broken and the only way is to restart kernel.
c

Carter

03/04/2022, 12:40 PM
So there might be a confluence of three issues: 1. Need to document putting
@file
in its own cell before import 2. Need to document using
-jvm
suffix for multiplatform libraries 3. Possibly some bug in the kernel that didn’t get cleared by re-running the cell. I’ll have to see if I can reproduce it
But the good news is that I’m unblocked so thank you for the response and the help.
i

Ilya Muradyan

03/04/2022, 12:55 PM
Re-running the cell shouldn't clear the state. It's by design. I'll document 1 and 2, thank you!
c

Carter

03/04/2022, 12:59 PM
I hit #2, then added -jvm, and then re-ran the cell that configured my import and it still failed. This didn’t meet my expectations as a user which then lead me to come to this Slack for help with the issue. I’m not sure what the right solution is, but this does make error recovery difficult.