Package pint seems to not for with Jupyter lab (Chapter 6.5)

Dear all,

right now I am working myself through the book “Python for the lab”. Yet I am almost done with chapter 6. But can’t continue due to an error that gets under my skin for the last few days.
In chapter 6.5 it is about performing a scan. We got introduced to the package pint that can handle units.
Sadly multiple problems occurre with this package “pint” while working with it.
When it isn’t the error about Quantities from different Registries another like this one I yet can’t fix:

DimensionalityError: Cannot convert from ‘0.0 volt’ to ‘volt’. Assign a quantity with the same dimensionality or access the magnitude directly as ‘obj.magnitude[0] = 0.0 volt’.

I tried to install an older pint version. Nothing works.
BUT when working only in the terminal it works. So somehow it doesn’t work with my Jupyter lab (Notebook). And I can’t imagine that this is in fact true. Maybbe I am missing out something very important??
Would be really nice if someone has a solution for this Problem.

All the best and thanks in advance

Hello Rebecka!

Welcome to the forum!

Can you give me an excerpt of the code you used to get the error? I’ll try to reproduce it and come back to you.

Also, what version of Pint and Jupyter are you using? Did you install them with conda or pip?

It may also be useful to log your issue directly on the official Pint repository, it may have been a problem introduced in a latest release and the developers may not be aware of it yet.

Hello Aqui,
thanks for your response. I hope that I solved the problem in my code so far. I tried to understand what I was doing wrong and I will try to explain it in the following. Maybe I can help someone else.
Besides tiny (frustrating) Syntax mistakes I needed to understand the base_dir, and the structure of importing pint.

All the following steps helped me out and are only reommendations for others.
First of all you should know that I work with Jupyter lab version 3.3.3 and since the book works with .py-Files, it was a little bit confusing for me at the beginning. To be able to import methods from other files I needed to recreate the notebooks that include the wanted method as Python files.
No big deal I just copy pasted my code into new .py-File with the same name as the notbeook.

With that being introduced, I can try to explain how I fixed the issue (hopefully ;)))

  1. in every notebook where I wanted to import a method from another pyton-File I have to include:

    import os
    import sys
    base_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath('__file__'))))

    here you can see that i had to add one more os.path.dirname() because I have one more folder than in the book explained and I had to include the parantheses around _ file_.
    I am not really sure why I need to add the parantheses but thats trial and error I guess : P

  2. I am working with pint version 0.18
    The most important: If you want to import pint you must import it firstly in the _ File in the root folder with

     import pint
     ur = pint.UnitRegistry()

    and if you not already included blank_ init files in every single folder where you want to import methods from, you should do that.

    And you should accept the fact that if you import pint at the top of your single notebook it won’t work, when you already worked with pint in another Python-file whoms methods you want to use in this notebook.

    Try to remember that pint should be imported once in the _ init file in the root folder and later you should import ur which contains this one and only Register for the units in all your notebooks from this root _init file.

  3. In my case the import always failed when I used following, because the notebook jumped to the
    _ init File in the folder I was currently in.

      from __init__ import ur

    Also the following try didn’t work (i named my root folder Code).

      from Code.__init__ import ur 

    What worked for me was simply to only write down the name of my root folder where _ init _ was saved:

     from Code import ur  

    And all of the sudden it somehow worked.

Now I am facing another error with numpy, but I wil try to work thru it today.

I hope this could help at least one person. I want to also highlight that all of what I wrote here is explained extremely well in the book. Maybe I didn’t read carefully enough which caused the mistakes.

All the best. And have fun with the book!!

1 Like

Thanks for the detailed answer. Indeed, the book is not designed to follow it with notebooks, and only some bits can be copy-pasted. The idea of the book is to build a complete package that can be reused and extended.

Many of the things you mentioned are problems with importing code from one file (or package) to another. This can definitely be daunting at the beginning. I wrote an article about it that may help you, just bear in mind it is not tailored for Jupyter notebook users, but many things follow the same logic.

I have been considering for a while introducing Jupyter into the blog/book, and I’ll do a pilot during a workshop in few weeks from now. Let’s see how it fits together and how much I can push through.

Good look with numpy, hope you solve It soon!

1 Like

Nice nice Nice!!!
Really excited about these news!

All the best and good look with the workshop.