wampthing2 wampthing2 - 1 year ago 116
Python Question

Calling xlwings from Excel "No module named..." Error

I'm trying to learn the ropes of starting a Python script from Excel VBA using xlwings 0.9.2. According to the docs, I need to change the

to the path for my py file. I've seen several versions of this question, and various answers, but none have addressed my specific scenario. I have a basic test module "module1.py" and an Excel file "Book2.xlsm" located on the desktop which has a button to run this macro:

Sub macro1()
RunPython ("import module1.py; module1.run_all()")
End Sub

module1 goes like this:

import xlwings as xw

def run_all():
xw.sheets("Sheet1").range("A1").value = "Done!"

I then imported the "xlwings.bas" file and edited the VBA code to read
PYTHONPATH = "C:\Users\bwamp\Desktop\module1\module1
, which refers to the subfolder that holds "module1.py" (full path: "C:\Users\bwamp\Desktop\module1\module1\module1.py", for clarity). Press the button to run
and I get the following error:


Traceback (most recent call last):

File "", line 1, in

ImportError: No module named 'module1.py'; 'module1' is not a package

Press Ctrl+C to copy this message to the clipboard.


Any ideas what I'm doing wrong?

Answer Source

You have two errors: You need to import your module without the .py ending:

Sub macro1()
RunPython ("import module1; module1.run_all()")
End Sub

And it's xw.Book.caller() instead of wb.Book.caller().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download