I’m trying to import the kotlinx datetime library ...
# datascience
c
I’m trying to import the kotlinx datetime library into a jupyter notebook. I thought I understand the syntax, but apparently not.
Copy code
@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
Use kotlinx-datetime-jvm. Maven resolver in the notebook can't us gradle metadata to automatically add suffixes for MPP libraries
c
I did try that also but it didn’t work for me either. Specifically
Copy code
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-datetime-jvm:0.3.2")
I’ve also tried creating a json descriptor and loading that.
Copy code
{
  "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
Copy code
%use @file[/data/notebook_files/datetime.json]
Which gives effectively the same error
Copy code
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
I don’t think I’ve ever seen that documented.
a
@Ilya Muradyan Probably it should be covered somewhere.
c
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
Yes, classpath needs to be reset if it is broken and the only way is to restart kernel.
c
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
Re-running the cell shouldn't clear the state. It's by design. I'll document 1 and 2, thank you!
c
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.
349 Views