How to split project into submodules



  • Apologies for the noob question. I've missed something but I can't figure out what.

    I'm using VS Code with pymakr plugin (on Linux). I would like to split my project into separate files to make things easier to manage, but I can't figure out how to bring everything together to run the project.

    I have a main.py, which is the main file (surprise) and another file called lookups.py, which has some dictionaries defined in it. I want to refer to the dictionaries from main.py, but I'm not sure how. I've tried every variation of "import" I can think of except for the right one apparently. What do I have to do to refer to another file in the project from the main file?

    All advice gratefully received.



  • @Gijs well that did it! thanks!!

    ;-)



  • Ah, I believe the problem is that you have to actually upload to device in order to use it. When you do that, it will upload the main.py, boot.py and /lib/lookups.py to the flash of your device and now they should be callable.



  • Hi @Gijs and thanks for the reply. That combination was one of the first I tried. I assumed that the lib folder was there to contain local libraries, so I put my submodule, "lookups.py", in it. However when I try to run my main.py with "import lookups" in the code I get an error "ImportError: no module named 'lookups'". I get the same error regardless of where I put the module.
    5f00316d-c1eb-4948-af85-efe162841b04-image.png



  • @mogplus8 said in How to split project into submodules:

    nt to refer to the dictionaries from main.py, but I'm not sure how. I've tried every variation of "import" I can think of except for the right one apparently. W

    Hi,
    I did it in the following way:

    • Create a folder called lib inside your project
    • In the folder, make a file (eg. thing.py) put your extra functions (do not forget to also copy the necessary imports)
    • use import thing in your main.py file and call the functions like thing.function(), instead you could also do import thing as anothername to use anothername.
      Let me know if that was clear!
      Gijs

Log in to reply
 

Pycom on Twitter