4

I have installed Python Tools for Visual Studio. I have also installed the latest Anaconda package. I'm a .NET developer but have a requirement to do some spreadsheet work with python.

So I develop and run my python code in Visual Studio with the aim being to eventually call it from excel using xlwings.

I've read about putting in a 'mock caller' so I wrote some code like this

if __name__ == '__main__':
    neto = exportcategories_api('Bellbird2')

    ## Expects the Excel file next to this source file, adjust accordingly.
    path = os.path.abspath(os.path.join(os.path.dirname(__file__),
                           'Export Categories.xlsm'))
    xw.Book.set_mock_caller(path)
    neto.export()

I have also included:

import xlwings as xw

at the top of my python file. This is underlined with 'unable to resolve'.

and when I run my code nothing happens. The command window pops up but it seems to hang.

If I remove the import xlwings as xw and comment out my mock caller lines of code, it runs fine.

What have I got wrong?

denfromufa
  • 5,610
  • 13
  • 81
  • 138
David
  • 1,203
  • 6
  • 25
  • 48
  • Sounds like you only need the mock caller when not running the code from Excel, so make the `import xlwings as xw` the first (indented) statement following the `if __name__ == '__main__':`. – martineau Aug 07 '16 at 10:07
  • If you're using xlwings >=0.9.0, then you have to use `xw.Book(path).set_mock_caller()`, that changed in comparison to the previous versions and the docs weren't initially updated correctly. – Felix Zumstein Aug 07 '16 at 10:20
  • In the python console (command prompt) if you type import xlwings - is it throwing any error? – be_good_do_good Aug 07 '16 at 11:16
  • No luck with the first two suggestions – David Aug 07 '16 at 11:27
  • Doesn't recognise 'import' but I can run a command such as Xlwings template open and it successfully opens excel with a Xlwings module – David Aug 07 '16 at 11:28
  • Are you sure about your python environment(and its freshness state)? Press CTRL+K Then Tilda(`) to view python enviroments, you can also open an interactive shell from within and then try the command in that shell like: import xlwings – lkn2993 Aug 07 '16 at 12:20
  • OK,I opened an interactive python shell and was able to do import xlwings without any error – David Aug 08 '16 at 10:26
  • Ok, a little embarrassing but it appears everything was just taking ages to spin up, close to a minute hence why I thought it was hanging. But in fact working ok – David Aug 09 '16 at 12:47

0 Answers0