importing own python modules?
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?
Comments are currently closed for this discussion. You can start a new one.
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
1 Posted by Stuart Beesley ... on 06 May, 2021 05:59 AM
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 Posted by Jim on 12 May, 2021 01:50 AM
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 Posted by Stuart Beesley ... on 12 May, 2021 08:00 AM
This method will work.. See attached main.zip
4 Posted by Jim on 14 May, 2021 03:29 AM
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 Posted by Stuart Beesley ... on 14 May, 2021 06:32 AM
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 Posted by J C on 14 May, 2021 07:33 PM
Thanks for the tip Stu.
I've not worked with extensions yet, but will keep that in mind.
JIm
System closed this discussion on 13 Aug, 2021 07:40 PM.