pyutils / line_profiler

Line-by-line profiling for Python
Other
2.68k stars 119 forks source link

Not able to run import polars as pl inside kernprof #262

Closed Chuck321123 closed 5 months ago

Chuck321123 commented 5 months ago

So if you toss in import polars as pl inside a code you will get an error that it cannot import lazyframe from polars. However, i am able to import perfectly fine in normal python. The command i use to run in anacoda promt after specifying path: kernprof -l "Line Profiler.py". My code:

from line_profiler import LineProfiler

@profile
def my_function():
    print("running")
    import pandas as pd
    import numpy as np
    import polars as pl

my_function()

Error message: ImportError: cannot import name 'LazyFrame' from 'polars.lazyframe.frame'

polars version=0.20.21 python=3.12.2 line_profiler=4.1.2 OS: Win-11

Erotemic commented 5 months ago

Works fine for me on Ubuntu with Python 3.11.2 and Python 3.12.3 (using pyenv not conda).

Also FYI, as of version 4.0 you can write your code like:

from line_profiler import profile

@profile
def my_function():
    print("running")
    import pandas as pd
    import numpy as np
    import polars as pl

my_function()

And the previous invocation kernprof works, but you can also set the environment variable LINE_PROFILE and run as normal:

LINE_PROFILE=1 python "Line Profiler.py"