1

I am having problems to organize the project structure and successfully run the program. I dont want to use the sys.path module, so I choose to create init.py files. But i notice redundancy and I am confused how to properly do this. Other packages need also modules of neighbored packages.

src
- __init__.py
- main.py
- data
-- __init__.py
-- data_loader.py
- util
-- __init__.py
-- stringUtil.py

_init_.py

import .data
import .util

main.py

from data import *
from utils import *

def __init__():
    print(StringUtil.substringRight("Hello world!", "l"))
    loader = DataLoader("C:/SOME/PATH/")

data/_init_.py

from .data import *

data/data_loader.py

from util.stringUtils import StringUtil

class DataLoader()
    def __init__(self, path):
        StringUtil.printHierarchyTree(path)

util/_init_.py

from .util import *

util/stringUtil

class StringUtil:
    @staticmethod
    def substringRight():
        ...

I just want to load all modules and classes upon start of the app.

It says:

File "main.py", line 5, in <module>
StringUtil.substringRight
NameError: name 'StringUtil' is not defined

also the Data Loader doesnt know StringUtil class

Mc Midas
  • 189
  • 1
  • 5
  • 17

1 Answers1

0

Shouldn't be in main.py instead of

from utils import *

this line?

from util import *

Anyway using

from some_package import * 

is always bad practise, you should always import only specific things instead of using asterisk.

tscunami
  • 45
  • 5
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 13 '21 at 14:55
  • Yeah I made a typo but the problem is the same. I understand your point but where should I make all the imports? If I am correct, then the init.py should only contain all the classes in the modules of the same directory to load all when importing in main "from util import *" since init.py is called due to calling the "util" folder. – Mc Midas Sep 14 '21 at 12:16
  • OK, so I tried to reproduce your problem and I have this solution. In the first case, you don't need __init__.py inside src folder, so you can remove it. Then inside `data\__init__.py` change import on this `from data.data_loader import DataLoader`, in `util\__init__.py` change import on this `from util.stringUtil import StringUtil`, and in `data_loader.py` on this `from util.stringUtil import StringUtil`. With these changes, I have no error with your code. For more information, I would recommend this article https://python-packaging-tutorial.readthedocs.io/en/latest/setup_py.html – tscunami Sep 14 '21 at 15:06