# AttributeError observed in sample\_func when executed from a different python source file

**URL:** <https://discuss.ray.io/t/attributeerror-observed-in-sample-func-when-executed-from-a-different-python-source-file/22271>\
**Category:** Ray Core\
**Created:** [April 10, 2025, 4:22am UTC](https://discuss.ray.io/t/attributeerror-observed-in-sample-func-when-executed-from-a-different-python-source-file/22271 "2025-04-10T04:22:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scngupta-dsp](https://avatars.discourse-cdn.com/v4/letter/s/c0e974/32.png) [@scngupta-dsp](https://discuss.ray.io/u/scngupta-dsp)\
**Post date:** [April 10, 2025, 4:22am UTC](https://discuss.ray.io/t/attributeerror-observed-in-sample-func-when-executed-from-a-different-python-source-file/22271/1 "2025-04-10T04:22:59Z")

</div>

**1. Severity of the issue: (select one)**  
 Medium: Significantly affects my productivity but can find a workaround.

**2. Environment:**

- Ray version: 2.43.0
- Python version: 3.11
- OS: WSL (Ubuntu-22.04-LTS)
- Cloud/Infrastructure: NA
- Other libs/tools (if relevant): NA

**3. What happened vs. what you expected:**

- Expected: Results should not be dependent upon how the source is organized
- Actual: Results are dependent on how the source is organized

I am observing a strange issue - when I execute my code from a single source file there are no issues observed. When I try to distribute the source code into different files I am observing a strange AttributeError

I have tried to isolate & explain the issue via a minimal example. Request if community could review and suggest what could be the potential problem

**WORKING CASE** : working\_main.py

```auto
# Minimal example: Working scenario
# Single source file 
import functools
import ray

# Decorator 
def test(f):
    print(f"{f. __name__ } registered")
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        wrapper.some_attribute = 42
        f(*args, **kwargs)
        return wrapper
    return wrapper

# Sample function: to be executed in a remote process
@test
def sample_func():
    print("This is a test function")

# Utility function
def run(custom_func: callable = None):
    ray.init()

    remote_actor = ray.remote(custom_func)
    sample_out = ray.get(remote_actor.remote())

    # Check if the attribute is available
    print(f"{sample_out.some_attribute=}")

if __name__ == " __main__":
    run(sample_func)

```

_Results_:

> $ python working\_main.py  
> sample\_func registered  
> 2025-03-25 20:38:47,801 INFO worker.py:1852 – Started a local Ray instance.  
> sample\_out.some\_attribute=42  
> (sample\_func pid=694106) This is a test function

**PROBLEM SCENARIO** : Source code in two files (problem\_main.py and problem\_helper.py)

_File: problem\_main.py_

```auto
# Minimal example: Problem scenario
# Some source in a helper file
from problem_helper import run
from problem_helper import sample_func

if __name__ == " __main__":
    run(sample_func)

```

_File: problem\_helper.py_

```auto
import functools
import ray

# Decorator 
def test(f):
    print(f"{f. __name__ } registered")
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        wrapper.some_attribute = 42
        f(*args, **kwargs)
        return wrapper
    return wrapper

# Sample function: to be executed in a remote process
@test
def sample_func():
    print("This is a test function")

# Utility function
def run(custom_func: callable = None):
    ray.init()

    remote_actor = ray.remote(custom_func)
    sample_out = ray.get(remote_actor.remote())

    # Check if the attribute is available
    print(f"{sample_out.some_attribute=}")

```

_Results:_

> $ python problem\_main.py  
> sample\_func registered  
> 2025-03-25 20:39:05,564 INFO worker.py:1852 – Started a local Ray instance.  
> Traceback (most recent call last):  
> File “/path/problem\_main.py”, line 7, in  
> run(sample\_func)  
> File “/path/problem\_helper.py”, line 27, in run  
> print(f"{sample\_out.some\_attribute=}")  
> ^^^^^^^^^^^^^^^^^^^^^^^^^  
> AttributeError: ‘function’ object has no attribute ‘some\_attribute’  
> (pid=694632) sample\_func registered  
> (sample\_func pid=694632) This is a test function

**SUMMARY** : I am confused why the attribute in the second case is missing

---

<div class="post-metadata">

**Author:** ![jjyao](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/jjyao/32/1799_2.png) [@jjyao](https://discuss.ray.io/u/jjyao)\
**Post date:** [April 18, 2025, 4:14pm UTC](https://discuss.ray.io/t/attributeerror-observed-in-sample-func-when-executed-from-a-different-python-source-file/22271/2 "2025-04-18T16:14:24Z")

</div>

Does it work if you don’t use Ray?

---

<div class="post-metadata">

**Author:** ![scngupta-dsp](https://avatars.discourse-cdn.com/v4/letter/s/c0e974/32.png) [@scngupta-dsp](https://discuss.ray.io/u/scngupta-dsp)\
**Post date:** [April 20, 2025, 7:15am UTC](https://discuss.ray.io/t/attributeerror-observed-in-sample-func-when-executed-from-a-different-python-source-file/22271/3 "2025-04-20T07:15:07Z")

</div>

Hello @jjyao

Thanks for your response. It works without ray - refer to the minimal example which has been updated to work with / without ray

I would appreciate if you could describe what could be the possible issue as this same example works when all the source code is put into a single file

_File: problem\_main.py_

```auto
# Minimal example: Problem scenario
# Some source in a helper file
import argparse
from problem_helper import run
from problem_helper import sample_func

if __name__ == " __main__":
    parser = argparse.ArgumentParser(description="Run sample_func with or without Ray.")
    parser.add_argument(
        "--use_ray",
        action="store_true",
        help="Use Ray for execution if this flag is set."
    )
    args = parser.parse_args()

    run(sample_func, use_ray=args.use_ray)

```

_File: problem\_helper.py_

```auto
import functools
import ray

# Decorator 
def test(f):
    print(f"{f. __name__ } registered")
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        wrapper.some_attribute = 42
        f(*args, **kwargs)
        return wrapper
    return wrapper

# Sample function: to be executed in a remote process
@test
def sample_func():
    print("This is a test function")

# Utility function
def run(custom_func: callable = None, use_ray: bool = True):

    if use_ray:
        ray.init()

        remote_actor = ray.remote(custom_func)
        sample_out = ray.get(remote_actor.remote())

        # Check if the attribute is available
        print(f"{sample_out.some_attribute=}")

    else:
        # Execute the function locally
        custom_func()

        # Check if the attribute is available
        print(f"{custom_func.some_attribute=}")

```

**Sample output**

_Without ray_

```auto
$ python problem_main.py
sample_func registered
This is a test function
custom_func.some_attribute=42

```

_With Ray_

```auto
$ python problem_main.py --use_ray
sample_func registered
2025-04-20 12:38:01,909 INFO worker.py:1841 -- Started a local Ray instance.
(pid=72175) sample_func registered
(sample_func pid=72175) This is a test function
Traceback (most recent call last):
  File "/path/problem_main.py", line 16, in <module>
    run(sample_func, use_ray=args.use_ray)
  File "/path/problem_helper.py", line 29, in run
    print(f"{sample_out.some_attribute=}")
             ^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'function' object has no attribute 'some_attribute'

```
