Description
Describe the bug
Custom python modules (folders) don't get compiled onto the hub. Everything works fine when I put every python file that gets imported by any other python file into the project root. However, when I try to import a file that's within a directory, this error occurs:
[tom@neon:~/Coding/2425-RobotCode]$ pybricksdev run ble main.py
Searching for any hub with Pybricks service...
Traceback (most recent call last):
File "/home/tom/Coding/2425-RobotCode/venv/bin/pybricksdev", line 8, in <module>
sys.exit(main())
^^^^^^
File "/home/tom/Coding/2425-RobotCode/venv/lib/python3.11/site-packages/pybricksdev/cli/__init__.py", line 383, in main
asyncio.run(subparsers.choices[args.tool].tool.run(args))
File "/home/tom/.nix-profile/lib/python3.11/asyncio/runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "/home/tom/.nix-profile/lib/python3.11/asyncio/runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tom/.nix-profile/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/home/tom/Coding/2425-RobotCode/venv/lib/python3.11/site-packages/pybricksdev/cli/__init__.py", line 207, in run
await hub.run(script_path, args.wait)
File "/home/tom/Coding/2425-RobotCode/venv/lib/python3.11/site-packages/pybricksdev/connections/pybricks.py", line 572, in run
mpy = await compile_multi_file(py_path, abi)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tom/Coding/2425-RobotCode/venv/lib/python3.11/site-packages/pybricksdev/compile.py", line 119, in compile_multi_file
finder.run_script(path)
File "/home/tom/.nix-profile/lib/python3.11/modulefinder.py", line 153, in run_script
self.load_module('__main__', fp, pathname, stuff)
File "/home/tom/.nix-profile/lib/python3.11/modulefinder.py", line 349, in load_module
self.scan_code(co, m)
File "/home/tom/.nix-profile/lib/python3.11/modulefinder.py", line 411, in scan_code
self._safe_import_hook(name, m, fromlist, level=0)
File "/home/tom/.nix-profile/lib/python3.11/modulefinder.py", line 367, in _safe_import_hook
self.import_hook(name, caller, level=level)
File "/home/tom/.nix-profile/lib/python3.11/modulefinder.py", line 165, in import_hook
q, tail = self.find_head_package(parent, name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tom/.nix-profile/lib/python3.11/modulefinder.py", line 221, in find_head_package
q = self.import_module(head, qname, parent)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tom/.nix-profile/lib/python3.11/modulefinder.py", line 308, in import_module
fp, pathname, stuff = self.find_module(partname,
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tom/.nix-profile/lib/python3.11/modulefinder.py", line 489, in find_module
return _find_module(name, path)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tom/.nix-profile/lib/python3.11/modulefinder.py", line 69, in _find_module
if spec.loader.is_package(name):
^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'is_package'
When I add an init.py file to the directory, it successfully uploads the file to the hub but fails importing it:
[tom@neon:~/Coding/2425-RobotCode]$ pybricksdev run ble main.py
Searching for any hub with Pybricks service...
100%|████████████████████████████████████████████████████████████████████████████████████████████████| 316/316 [00:00<00:00, 2.14kB/s]
Traceback (most recent call last):
File "main.py", line 1, in <module>
ImportError: can't import name actual_module
However, when I remove the actual_module file and put the functions into the init.py file and remove the actual_module part from the import statement, everything works fine (except for my IDE not picking up the function)
To reproduce
Steps to reproduce the behavior:
- Create a pybricks project
- Create main file
- Create module
- Import module in main
Expected behavior
It compiles every single file successfully, and it runs without any issues.
I hope my explanation of this issue was sufficient, and thanks in advance!