importing own python modules?

Jim's Avatar


06 May, 2021 01:51 AM

Haven't figured out a way to do this, or if it's even really possible within MD, but can I create a python module and import it to use in another module?

  1. 1 Posted by Stuart Beesley ... on 06 May, 2021 05:59 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Do you mean when running via moneybot or when running the Python script within an extension mxt file?

    Do you really need an import, would an ‘exec’ do the job?

    Import requires the module to be in the current directory or in the path.. but I know there can be problems...

    What problems are you seeing?

  2. 2 Posted by Jim on 12 May, 2021 01:50 AM

    Jim's Avatar

    Hi, Thanks for the response...

    I'm just just talking about a plain script, not an mxt file. I've no experience with mxt files.

    On exec vs import...admittedly I've never used exec, but will look into that.

    On the import problem...for example i create a module with 1 function. I create another module to import the first module. I import the function and call the function. This works all fine outside of MD when I just run from the command line, but from MD I get:

    Beginning file D:\DataDrive\Jim\Documents\GitHub\moneydance\ at Tue May 11 19:37:00 MDT 2021
    Error running script: Traceback (most recent call last):
      File "D:\myfilepath\", line 4, in <module>
        import testcsvread
    ImportError: No module named testcsvread
    Traceback (most recent call last):
      File "D:\DataDrive\Jim\Documents\GitHub\moneydance\", line 4, in <module>
        import testcsvread
    ImportError: No module named testcsvread

  3. 3 Posted by Stuart Beesley ... on 12 May, 2021 08:00 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    This method will work.. See attached

  4. 4 Posted by Jim on 14 May, 2021 03:29 AM

    Jim's Avatar

    Thanks Stu.
    Since your last name I did more looking around and have figured out this also works:\

    import os
    path = os.path.dirname(os.path.abspath(__file__))

    import imp
    foo = imp.load_source('', os.path.join(path,''))
    print (type(foo.readCSV))

    Any idea what pros/cons each method has?


  5. 5 Posted by Stuart Beesley ... on 14 May, 2021 06:32 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    I think you know enough to read the docs and find out which method works best for you. I see you are relying on file. For example if you were running as an a extension, this variable is not set.

  6. 6 Posted by J C on 14 May, 2021 07:33 PM

    J C's Avatar

    Thanks for the tip Stu.

    I've not worked with extensions yet, but will keep that in mind.


  7. System closed this discussion on 13 Aug, 2021 07:40 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac