alxdrcirilo / apple-health-parser

Python package to parse, analyse, and plot Apple HealthKit data
http://alxdrcirilo.dev/apple-health-parser
MIT License
7 stars 2 forks source link

ImportError: cannot import name 'Plot' from 'apple_health_parser.plot' #2

Closed ScottM42 closed 3 months ago

ScottM42 commented 3 months ago

This looks like a great project, thanks for your efforts!

After install using pip with Python 3.12 I'm able to use the Parser class, yet the Plot class fails to import with a generic and uninformative error. I'm opening this issue because the install 'mostly works', but Plot does not - is there a circular import? (I apologize if this is operator error)

Describe the bug ImportError: cannot import name 'Plot' from 'apple_health_parser.plot'

To reproduce pip install apple_health_parser % cat testplot.py

from apple_health_parser.plot import Plot
print(Plot)

% python testplot.py Traceback (most recent call last): File "/Users/scott/bin/applehealth/testplot.py", line 1, in from apple_health_parser.plot import Plot ImportError: cannot import name 'Plot' from 'apple_health_parser.plot' (/Users/scott/bin/applehealth/lib/python3.12/site-packages/apple_health_parser/plot/init.py)

Here's an example of the installation 'mostly working'

% cat testparser.py

from apple_health_parser.utils.parser import Parser
print(Parser)

% python testparser.py <class 'apple_health_parser.utils.parser.Parser'>

% cat testextract2.py

from apple_health_parser.utils.parser import Parser

parser = Parser(export_file="/Users/scott/bin/applehealth/export.zip", overwrite=True)
print(parser.flags)
sources = parser.get_sources(flag="HKQuantityTypeIdentifierHeartRate")
print(sources)
data = parser.get_flag_records(flag="HKQuantityTypeIdentifierHeartRate")

python testextract2.py Deleted previous export at data/apple_health_export... ['HKCategoryTypeIdentifierAppleStandHour', 'HKCategoryTypeIdentifierAudioExposureEvent', 'HKCategoryTypeIdentifierMindfulSession', 'HKQuantityTypeIdentifierActiveEnergyBurned', 'HKQuantityTypeIdentifierAppleExerciseTime', 'HKQuantityTypeIdentifierAppleStandTime', 'HKQuantityTypeIdentifierAppleWalkingSteadiness', 'HKQuantityTypeIdentifierBasalEnergyBurned', 'HKQuantityTypeIdentifierBodyFatPercentage', 'HKQuantityTypeIdentifierBodyMass', 'HKQuantityTypeIdentifierBodyMassIndex', 'HKQuantityTypeIdentifierDistanceCycling', 'HKQuantityTypeIdentifierDistanceDownhillSnowSports', 'HKQuantityTypeIdentifierDistanceWalkingRunning', 'HKQuantityTypeIdentifierEnvironmentalAudioExposure', 'HKQuantityTypeIdentifierFlightsClimbed', 'HKQuantityTypeIdentifierHeadphoneAudioExposure', 'HKQuantityTypeIdentifierHeartRate', 'HKQuantityTypeIdentifierHeartRateRecoveryOneMinute', 'HKQuantityTypeIdentifierHeartRateVariabilitySDNN', 'HKQuantityTypeIdentifierHeight', 'HKQuantityTypeIdentifierLeanBodyMass', 'HKQuantityTypeIdentifierNumberOfTimesFallen', 'HKQuantityTypeIdentifierOxygenSaturation', 'HKQuantityTypeIdentifierPhysicalEffort', 'HKQuantityTypeIdentifierRespiratoryRate', 'HKQuantityTypeIdentifierRestingHeartRate', 'HKQuantityTypeIdentifierRunningGroundContactTime', 'HKQuantityTypeIdentifierRunningPower', 'HKQuantityTypeIdentifierRunningSpeed', 'HKQuantityTypeIdentifierRunningStrideLength', 'HKQuantityTypeIdentifierRunningVerticalOscillation', 'HKQuantityTypeIdentifierSixMinuteWalkTestDistance', 'HKQuantityTypeIdentifierStairAscentSpeed', 'HKQuantityTypeIdentifierStairDescentSpeed', 'HKQuantityTypeIdentifierStepCount', 'HKQuantityTypeIdentifierTimeInDaylight', 'HKQuantityTypeIdentifierVO2Max', 'HKQuantityTypeIdentifierWalkingAsymmetryPercentage', 'HKQuantityTypeIdentifierWalkingDoubleSupportPercentage', 'HKQuantityTypeIdentifierWalkingHeartRateAverage', 'HKQuantityTypeIdentifierWalkingSpeed', 'HKQuantityTypeIdentifierWalkingStepLength']

A similar error happens with the included main.py script with other Classes

% python ./main.py Traceback (most recent call last): File "/Users/scott/bin/applehealth/lib/python3.12/site-packages/apple_health_parser/scripts/./main.py", line 3, in from apple_health_parser.plot.overviews import ActivityOverview ImportError: cannot import name 'ActivityOverview' from 'apple_health_parser.plot.overviews' (/Users/scott/bin/applehealth/lib/python3.12/site-packages/apple_health_parser/plot/overviews.py)

Fix Ok, the fix is to add import statements into the plot/init.py file, this way, the importer knows which file the Plot class is defined in. Maybe there is another way around this issue, but this fixes it on my installation:

plot % cat __init__.py

from .plots import Plot
from .overviews import Overview
alxdrcirilo commented 3 months ago

Hi 👋

Thank you for submitting a detailed issue :)

This is definitely not an operator error. There were already circular imports before and I tried to use absolute imports to fix this. The problem here was that the documentation was misleading as it has examples referencing imports when the code was still using imports such as the one defined in your __init__.py example.

I added the changes you suggested, this should not cause any other issue. Also fixed the script.py since plotting an activity overview plot requires the same Overview class with an additional argument overview_type="activity".

Thanks for using apple-health-parser!

ScottM42 commented 3 months ago

Great, thank you!

On Aug 17, 2024, at 1:23 AM, Alexandre Cirilo @.***> wrote:

Hi 👋

Thank you for submitting a detailed issue :)

This is definitely not an operator error. There were already circular imports before and I tried to use absolute imports to fix this. The problem here was that the documentation was misleading as it has examples referencing imports when the code was still using imports such as the one defined in your init.py example.

I added the changes you suggested, this should not cause any other issue. Also fixed the script.py since plotting an activity overview plot requires the same Overview class with an additional argument overview_type="activity".

Thanks for using apple-health-parser!

— Reply to this email directly, view it on GitHub https://github.com/alxdrcirilo/apple-health-parser/issues/2#issuecomment-2294791981, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJGASYPKGYBLUJBGNYF27SLZR4CBXAVCNFSM6AAAAABMUTLS36VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOJUG44TCOJYGE. You are receiving this because you authored the thread.