user2665694
user2665694

Reputation:

Python units module for conversion between liters and milliliters not working

Using the 'units module for performing basic conversions between units. However it does not seem to be possible to perform an easy conversion between liters and milliliters...why?

>>> from units import unit

>>> one_liter = unit('L')(1)   
>>> one_liter
Quantity(1, LeafUnit('L', False))

>>> unit('mL')(one_liter)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/heinz/src/units/lib/python2.6/site-packages/units/abstract.py", line 23, in __call__
    raise IncompatibleUnitsError()
units.exception.IncompatibleUnitsError

Upvotes: 2

Views: 2409

Answers (1)

Paul Hankin
Paul Hankin

Reputation: 58339

You need to call define_units() in the predefined sub-module to use this package.

import units
import units.predefined

units.predefined.define_units()
one_liter = unit('L')(1)
unit('mL')(one_liter)

I think this package could have been better designed -- as you've discovered, it's easy to accidentally use user-defined units rather than standard ones.

Upvotes: 2

Related Questions