ericremoreynolds / excelpython

An open source, easy to use interface for calling Python code from Excel
http://ericremoreynolds.github.io/excelpython
BSD 2-Clause "Simplified" License
234 stars 58 forks source link

Updating links to revised Python code #16

Closed DougAJ4 closed 10 years ago

DougAJ4 commented 10 years ago

Working through the tutorials, it seems that the "Import Python UDFs" button automatically generates new VBA code, writing over whatever we had before. Is there any way to reload the Python code without changing the VBA in any way? At the moment I am having to close Excel and re-open.

ericremoreynolds commented 10 years ago

I am surprised that you are having to restart Excel, actually the Python script (the one with the same name as the workbook) should be automatically reloaded by ExcelPython every time it is changed.

The same is not true of modules that the script file depends on, those do not get reloaded automatically.

If you explain better how your code is structured, perhaps we can figure out a way to make the reload automatic?

charliegenge commented 10 years ago

I am also having to restart excel to have my python code changes recognised in an Excel UDF. I tried changing py.module("python module with same name as workbook",True) and it still doesn't load the changes. The code changes are in a module which is imported by "python module with same name as workbook".py. Is this the expected behaviour? How can I work around this?

ericremoreynolds commented 10 years ago

Hi Charlie,

Are you using the add-in UDF functionality or are you writing the VBA wrapper manually?

If you are using the add-in I am bit confused by your comment about the line Py.Module("python module with same name as workbook", True), because the add-in doesn't actually load the WorkbookName.py file as a module. Can you please show me the VBA code you're referring to and also tell me which release of ExcelPython you're using.

Regards,

Eric.

charliegenge commented 10 years ago

Hi Eric, thanks for the rapid reply! Sorry, what I originally wrote was incorrect. I am using the add-in UDF functionality. I have a workbook called "wbk.xlsm" for example, along with the python script "wbk,py" in the same directory. The wbk.py script imports another python script called "mysql.py" for example. If I make changes to the "mysql.py" script, clicking on the 'import Python UDFs' toolbar button does not load these changes, unless I restart excel. I tried to change the line in the VBA code from 'Set xlpy = Py.Module("xlpython")' to 'Set xlpy = Py.Module("xlpython", True)' and this does not help. Is there anything else I could be doing to load the changes in the script? I'm using Excel 2010 and python 2.7 and excelpython 2.06. Thanks!

ericremoreynolds commented 10 years ago

Ok got it, what you are experiencing is in fact the expected behaviour. The wbk.py file will automatically be reloaded when it changes, but referenced modules will not be reloaded.

This is really a feature of Python rather than ExcelPython, because ExcelPython's module reloading functionality works the same way as Python's reload function. There are various Python libraries around which keep track of module changes and automatically reload them but TBH there is always a risk that your modules will get out-of-sync or that unexpected behaviour will arise, so the best thing is to restart Python.

I am thinking that the best solution would be for the ExcelPython toolbar to have a 'Restart Python' button which would kill the Python runtime and launch it again, so that all modules then get reloaded.

By the way, right now you don't need to shut down Excel but you can just kill pythonw.exe from Task Manager, it will get automatically restarted.

charliegenge commented 10 years ago

Hi Eric, that's great, thanks very much for the detailed response.