0

Im quite new to python packages. I'm currently programming in Python 3+ I have a project structure with different small files in a folder called 'Libraries' and 1 python file called Main.py.

My project structure: Project structure

I have the following import structure:

#Main.py
from Libraries.ATV320 import ATV320
x = ATV320()
.....

#ATV320.py
from ModbusSerial import SerialClient
class ATV320:
......

#ModbusSerial.py
import time
class SerialClient:
......

If I run SerialClient.py i get no errors, if i run ATV320.py I get no errors. When running main.py i get following error:

  File "c:\Users\public\Documents\GitHub\Project\Main.py", line 2, in <module>
    from Libraries.ATV320 import ATV320
  File "c:\Users\public\Documents\GitHub\Project\Libraries\ATV320.py", line 1, in <module>
    from ModbusSerial import SerialClient
ModuleNotFoundError: No module named 'ModbusSerial'

Anyone know what I am doing wrong? Since the two library files work fine when executed... When i run main.py its suddenly throws an import error?

Yorick
  • 51
  • 8
  • Does it work with `from .ModbusSerial import SerialClient` (note the dot)? See https://docs.python.org/3/reference/import.html#package-relative-imports – mzjn Feb 06 '23 at 16:02
  • Hey, yes that solves it for the main.py but results in error when running aTV320.py: ```from .ModbusSerial import SerialClient ImportError: attempted relative import with no known parent package``` – Yorick Feb 07 '23 at 07:58
  • There are many similar questions, for example https://stackoverflow.com/q/16981921/407651 and https://stackoverflow.com/q/14132789/407651 – mzjn Feb 07 '23 at 08:06

0 Answers0