0

I'm working on a translation (don't know if this is the correct term) of a code from MatLab to Python. While I can take advantage of the differences between the two languages it has been asked me to keep the file/folder structure the same, but right now I'm having some problems with the import of functions.

Every file is contained in one folder, that contains a file main.py and a sub folder programs. In programs I have a file called do_stuff.py and another folder Data. In Data I have one last file called DeviceData.py.

The files are something like this:

DeviceData.py:

def FuncDeviceData():
    x=5
    return x

do_stuff.py:

def FuncDo_Stuff():
    y=DeviceData()+1
    print(y)

main.py:

FuncDo_Stuff()
   # TBN: code is oversimplified for clarity.

In the MatLab version there was a file used to add to the path all the subfolders but I honestly don't know how to do the same in python, or to do it "the python way". I have tried the usual from file import func but I cannot find a proper solution.

P.S. I don't usually work with python, I'm kinda doing this for fun and it could be useful for the overall project.

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

2

Make a new python file __init__.py and keep it to programs folder. Then you can able to import functions from others python file to main.py .

Here is the folder structure:

── main_poject_folder
    ├── main.py
    └── programs
        ├── DeviceData.py
        ├── DoStaff.py
        └── __init__.py
barii
  • 339
  • 3
  • 12