benfred / py-spy

Sampling profiler for Python programs
MIT License
12.89k stars 431 forks source link

Handling paths set by shell environment variables #674

Open aditya-sengupta opened 3 months ago

aditya-sengupta commented 3 months ago

I'm trying to profile code that uses paths set by environment variables within zsh. Within py-spy these paths all return None.

MWE: in test_pyspy.py,

import os
import numpy as np
import time

np.load(os.path.join(os.environ.get("pyspy_testpath"), "test.npy"))
time.sleep(5) # to give profiling time to collect samples

With the existence of pyspy_testpath set by export pyspy_testpath="/Users/adityasengupta/projects/misc" and the existence of test.npy at that path, python3 test_pyspy.py runs as expected. But py-spy has the following output:

>>> sudo py-spy record -- python3 test_pyspy.py                                    
py-spy> Sampling process 100 times a second. Press Control-C to exit.

Traceback (most recent call last):
  File "/Users/adityasengupta/projects/misc/test_pyspy.py", line 5, in <module>
    np.load(os.path.join(os.environ.get("pyspy_testpath"), "test.npy"))
  File "/opt/homebrew/Cellar/python@3.10/3.10.14_1/Frameworks/Python.framework/Versions/3.10/lib/python3.10/posixpath.py", line 76, in join
    a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not NoneType

py-spy> Stopped sampling because process exited
py-spy> Wrote flamegraph data to 'python3-2024-08-07T14:29:49-07:00.svg'. Samples: 19 Errors: 0
python3-2024-08-07T14:29:49-07:00.svg?                                                                                                                     
[0]     cancel
[1]     Open the file python3-2024-08-07T14:29:49-07:00.svg
[2]     Open the URL  python3-2024-08-07T14:29:49-07:00.svg

Which did you mean? Cancelled.

It's not feasible to rewrite the code to avoid these references as it's a large package, is there a workaround for this?

Using py-spy 0.3.14, Python 3.10.14 on a MacBook M1 Pro running MacOS Ventura 13.2.1.