importing own python modules?

Jim's Avatar

Jim

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 - JUST A FELLOW USER and Toolbox ‘guy’'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\test-import.py at Tue May 11 19:37:00 MDT 2021
    Error running script: Traceback (most recent call last):
      File "D:\myfilepath\test-import.py", line 4, in <module>
        import testcsvread
    ImportError: No module named testcsvread
    Traceback (most recent call last):
      File "D:\DataDrive\Jim\Documents\GitHub\moneydance\test-import.py", 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 - JUST A FELLOW USER and Toolbox ‘guy’'s Avatar

    This method will work.. See attached main.zip

  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('module.name', os.path.join(path,'testcsvread.py'))
    print (type(foo.readCSV))
    foo.readCSV()

    ----
    Any idea what pros/cons each method has?

    Thanks,
    Jim

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

    Stuart Beesley - JUST A FELLOW USER and Toolbox ‘guy’'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.

    JIm

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? 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