# Runtime\_env "py\_modules" fails for modules which aren't packages

**URL:** https://discuss.ray.io/t/runtime-env-py-modules-fails-for-modules-which-arent-packages/5183
**Category:** Ray Core
**Created:** [February 23, 2022, 7:39pm UTC](https://discuss.ray.io/t/runtime-env-py-modules-fails-for-modules-which-arent-packages/5183 "2022-02-23T19:39:11Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ncoish](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/ncoish/32/2206_2.png) [@ncoish](https://discuss.ray.io/u/ncoish)
#### Post date: [February 23, 2022, 7:39pm UTC](https://discuss.ray.io/t/runtime-env-py-modules-fails-for-modules-which-arent-packages/5183/1 "2022-02-23T19:39:11Z")

</div>

Hi all,  
I’m developing a tool which allows users to run their own compute plugins which uses Ray as a remote backend.

I’m trying to improve the developer experience for plugin writers so that they can easily test that their code is working. The runtime\_env recommendations have been very helpful:  
[https://docs.ray.io/en/latest/handling-dependencies.html#library-development](https://docs.ray.io/en/latest/handling-dependencies.html#library-development)

A problem I’m running into right now is that, if I try to create a runtime environment with a module which is **not a package** , then the connection attempt fails with the following error:

```auto
...
    conn = ray.connect(
  File "/.../lib/python3.9/site-packages/ray/util/client/ __init__.py", line 228, in connect
    conn = self.get_context().connect(*args, **kw_args)
  File "/.../lib/python3.9/site-packages/ray/util/client/ __init__.py", line 88, in connect
    self.client_worker._server_init(job_config, ray_init_kwargs)
  File "/.../lib/python3.9/site-packages/ray/util/client/worker.py", line 681, in _server_init
    runtime_env = upload_py_modules_if_needed(
  File "/.../lib/python3.9/site-packages/ray/_private/runtime_env/py_modules.py", line 59, in upload_py_modules_if_needed
    if len(module. __path__ ) > 1:
AttributeError: module 'my_module' has no attribute ' __path__'

```

This is the connection logic I’m using:

```auto
import my_module

job_config = ray.job_config.JobConfig(
    runtime_env={
        "py_modules": [my_module]
    }
)
ray.util.connect(f"{host}:{port}", metadata=headers, secure=use_tls, job_config=job_config)

```

Where `my_module` is an imported python file which is not part of a package. If instead I used something like `my_package` which is a directory containing an ` __init__.py`, then the upload works properly.

Is this a bug, or a limitation? Or am I just doing something wrong?

Thank you!

---

<div class="post-metadata">

### Author: ![yic](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/yic/32/437_2.png) [@yic](https://discuss.ray.io/u/yic)
#### Post date: [February 23, 2022, 10:05pm UTC](https://discuss.ray.io/t/runtime-env-py-modules-fails-for-modules-which-arent-packages/5183/2 "2022-02-23T22:05:21Z")

</div>

Yes, right now, it needs ` __init__.py`, because it’s about a module.

I think maybe we can expand this a little bit. We probably can use `. __spec__ ` and use `origin` and `submodule_search_locations`.

```auto
>>> import ray
ray.>>> ray. __spec__
ModuleSpec(name='ray', loader=<_frozen_importlib_external.SourceFileLoader object at 0x7f2362bd1640>, origin='/mnt/yic/ray/python/ray/ __init__.py', submodule_search_locations=['/mnt/yic/ray/python/ray'])

```

So for the case in the OP, `submodule_search_locations` shouldn’t be there and we only copy `origin`.

@architkulkarni Do you have some suggestions about this?

---

<div class="post-metadata">

### Author: ![architkulkarni](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/architkulkarni/32/10_2.png) [@architkulkarni](https://discuss.ray.io/u/architkulkarni)
#### Post date: [February 23, 2022, 11:41pm UTC](https://discuss.ray.io/t/runtime-env-py-modules-fails-for-modules-which-arent-packages/5183/3 "2022-02-23T23:41:42Z")

</div>

Hi @ncoish, yes, this is a limitation. Could you please make a request for this as a Ray Github issue?

I don’t know too much about the details here, but Yi’s suggestion sounds reasonable and looks like a good place to start.

---

<div class="post-metadata">

### Author: ![architkulkarni](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/architkulkarni/32/10_2.png) [@architkulkarni](https://discuss.ray.io/u/architkulkarni)
#### Post date: [February 23, 2022, 11:44pm UTC](https://discuss.ray.io/t/runtime-env-py-modules-fails-for-modules-which-arent-packages/5183/4 "2022-02-23T23:44:28Z")

</div>

In the meantime, if your module is just a file, you should be able to upload it as part of the `working_dir`, or just include the path to the directory containing the file in the `py_modules` field–does the import work in that case?
