#๐Ÿ”’ Includes not working as expected

21 messages ยท Page 1 of 1 (latest)

shy epoch
#

I am having issues calling a function which is in a separate file contained in the subfolder.

Directory structure is attached.

The file is msgTypesPy/cam/camHandler.py. If I run it alone, everything works as expected.

import json
import cam

def decode():
    value = json.loads(r'{"............."}', parse_float=str)

    try:
        encoded = cam.CAM_PDU_Descriptions.CAM.encode('UPER', value)

        print('Encoded HEX: ' + ', '.join(['0x{:02X}'.format(val) for val in encoded]))
        print()

        decoded = cam.CAM_PDU_Descriptions.CAM.decode('UPER', encoded)

        print('Decoded value: {}'.format(json.dumps(decoded, default=str)))
        print()

    except NotImplementedError as e:
        print('Not Implemented Error : {}'.format(format(e)))

decode()

If I run messageHandler.py which has the following code:

from msgTypesPy.cam import camHandler
camHandler.decode()

it fails with an error:

Traceback (most recent call last):
  File "c:/Users/Srdan/Desktop/Dev_C-ITS/messageHandler copy.py", line 1, in <module>
    from msgTypesPy.cam import camHandler
  File "c:\Users\Srdan\Desktop\Dev_C-ITS\msgTypesPy\cam\camHandler.py", line 2, in <module>
    import cam
ModuleNotFoundError: No module named 'cam'

Could you help me and explain why the module is not found when the function is called from another file?
I really do not understand what the issue here is...

neon yachtBOT
#

@shy epoch

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

austere maple
#

your project structure looks wrong on two points: 1) your project root directory should not contain any code, 2) your packages are missing __init__.py modules

#

put all other files like licenses in the root directory, and structure your code under a single top-level package

#

your import is specifically failing because it tries to find a top level module named cam

#

and at least to me, it's not clear which packages are supposed to be top level and which are not

#

so fix the confusing package structure first

shy epoch
#

Thanks!

#

Not sure if I got this quite right

#

So this would be the whole project:

#

So you propose to put everything code related into a single folder in the root

#

Like this?

#

Ok, now I have restructured everything and renamed the package, but I still have the same error

#

The messageHandler copy.py calls the camHandler properly

from msgTypesPy.cam1 import camHandler
camHandler.decode()
#

But it then can't find the module cam.

If I start camHandler.py directly, it works well.
I have absolutely no understanding what I am doing wrong here..

shy epoch
#

Could someone help me out?

neon yachtBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.