"NameError: name 'moneydance' is not defined" after import

md_oz's Avatar

md_oz

17 Apr, 2019 09:47 PM

I have a modified version of the set_prices.py script (https://infinitekind.com/dev/set_prices.py), as shown below.

book = moneydance.getCurrentAccountBook()
currencies = book.getCurrencies()
print(moneydance)
def setPriceForSecurity(symbol, price, dateint):
  price = 1/price
  security = currencies.getCurrencyByTickerSymbol(symbol)
  if not security:
    print "No security with symbol/name: %s"%(symbol)
    return
  if dateint:
    security.setSnapshotInt(dateint, price).syncItem()
  security.setUserRate(price)
  security.syncItem()
  print "Successfully set price for %s"%(security)


def main(): setPriceForSecurity('AAPL', 219.9175, 0) return


if name == 'main': main()
When I 'Open Script' and 'Run' in the MoneyBot interface it successfully executes and "Successfully set price for Apple Inc." message is displayed on the console.

I have another program, set_prices_via_import.py, which imports the set_prices.py script and then executes it. I've simplified/sanitized the script below:

import sys
from os.path import expanduser


import_path = expanduser("~") + r"\Desktop\Now\Code\Python\Unit\MoneyBot\wip" # set_price.py location sys.path.insert(0, import_path) # add set_price.py location to the python path


print(type(moneydance), moneydance) # check that moneydance is available


import set_prices # import set_prices.main() # execute
When I run this script via MoneyBot it fails to execute yielding a "NameError: name 'moneydance' is not defined" error.

By investigating moneydance in the successful script I see that moneydance == com.moneydance.apps.md.controller.Main@147efb4d.

I've tried passing a global variable, importing the controller, but I haven't found the right approach/syntax.

Any suggestions on how do I remedy this?

Thank you.

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