I'm starting a project in python, the code structure now as below:
project/
__init__.py
a.py
b.py
mainA.py
utilities/
__init__.py
mainB.py
c.py
The __init__
files are all blank.
I want to run utilities/mainB.py
as a program(using something like python main.py
), and mainB needs to import a.py
and b.py
. So I tried from .. import a
and some other approaches, but the import
failed. The error information is:
ValueError: Attempted relative import in non-package
So here comes the questions:
- how to fix
mainB.py
so it can be run as a main program? mainA.py
can be run as main program now, it also importsa.py
andb.py
(usingimport a
andimport b
). I think the code structure may become more complex. Say, ifmainA.py
has to import a module fromproject/some/directory
, how can I do that?