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

Import problem (virtualenv) #6

Closed EricE closed 10 years ago

EricE commented 10 years ago

Can't import packages located in a virtualenv's site-packages directory.

https://sourceforge.net/p/excelpython/discussion/general/thread/3112cefd/

ericremoreynolds commented 10 years ago

Hi Eric,

As I was saying on SourceForge, one of the advantages of v2 is that because Python runs out-of-process, you can configure how the Python process is launched via the xlpython.cfg file. I am not very familiar with virtualenv, so your help in exploring how to use it in conjunction with ExcelPython is very valuable.

So assuming you have created your .xlsm file and in the same folder you have the xlpython folder, the xlpython.cfg file in there will dictate which Python executable is used for your workbook.

The file itself should be pretty self-explanatory, however in your case the important statement is

# The command line used to launch the COM server
Command = pythonw.exe -u "$(ConfigDir)\xlpyserver.py" $(CLSID)

pythonw.exe should be changed the path to your virtualenv executable C:\Path\To\ENV\bin\pythonw.exe. Does virtualenv support pythonw.exe? If not you can try using python.exe in the meantime to see if it works, even though it will show an ugly command line window.

Let me know how you get on.

Eric.

EricE commented 10 years ago

Hi Eric, Sorry to be late in replying. Sadly writing python isn't part of my job description and it got a bit busy. I changed the Command entry to:

Command = D:\SVNRepo\projects-sw\test\trunk\virtenv\Scripts\pythonw.exe -u "$(ConfigDir)\xlpyserver.py" $(CLSID)

And the example below ran!

But then something weird happened. I edited the python script to change the macro output (see below) and the output didn't change until I shut down and restarted Excel. Initially I thought that python wasn't noticing that the py file had changed so it wasn't updating the pyc file but I deleted the pyc file and the macro continued to run fine (pyc wasn't recreated).

I can't find a cached copy of the python script, is it being cached in memory somehow?

Also, I had to remove Py.AddPath from:

Set m = Py.Module("aaaa", _ Py.AddPath("..long_path...\virtenv\Lib\site-packages\requests"))

was getting a "Object doesn't support this property or method" error

Thanks, Eric PS example from SF post, modified to remove Py.AddPath :

Simple test script: aaaa.py import requests # in virtualenv site-packages, not main python install, comment out and it runs

def DoubleSum(a, b): return 2(a+b) #<--- changed this to 3 and the output didn't changed until I restarted Excel

Sub test() Set m = Py.Module("aaaa") MsgBox Py.Str(Py.Call(m, "DoubleSum", Py.Tuple(1, 2))) End Sub

ericremoreynolds commented 10 years ago

Ah excellent thanks for your feedback much appreciated!

The behaviour you are observing is because, as mentioned in the 2.0.2 release notes

Do you think these changes make sense? I changed the Py.Module behaviour because now with the add-in, in many cases people will be using that system for writing udfs in Python, which has an automatic reload mechanism, leaving the low level VBA API for more specialized bespoke tasks. I'm very interested in your opinion on these changes!

EricE commented 10 years ago

Hi Eric, I will spend more time working on my app this weekend so may have more comments later... Putting the pythonpath configuration in the .cfg file seems to work well. I can't think of a case where I have dynamically added to the pythonpath but I think it does come up. Can sys.path still be edited by the app?

I am initially planning on using Excel as essentially the UI for a python app so perhaps I'm not a typical use case (click a button and generate an entire tab). I can see how during development I would want reload to be True and then when done turn it off globally. Could it also be a setting in the .cfg file? Not a big deal for me, Wing makes it easy to find and update when I'm done.

Once I got the simple test app to run I tried my larger app and am getting an error. Don't have time until the weekend to troubleshoot it but how do you troubleshoot issues? I tried changing my python path to python.exe vs. pythonw.exe so I'd get a command window and then put in print statements but they don't show up. I also tried import pdb; pdb.set_trace() but it apparently doesn't play nicely with pywin32. Otherwise I'm returning a value to the vba code and using debug.print.

Thanks, Eric

ericremoreynolds commented 10 years ago

I see, lots of things for me to investigate then!

The sys.path can be manipulated dynamically - generally speaking anything that can be done in Python code can be done using the low-level VBA API.

I guess a .cfg setting could be added. But tbh its just as easy to edit the xlpyserver.py file in the local (workbook folder-specific) xlpython folder.

EricE commented 10 years ago

Hi Eric, To be clear (since I'll be deep into it soon), for debugging it is best to return values to the VBA macro and display the info?

Thanks, Eric

ericremoreynolds commented 10 years ago

Hi sorry my phone ran out of battery half way through the last message.

Changing pythonw.exe to python.exe should indeed cause the console window to appear, on my PC it does (maybe try commenting out the RedirectOutput line?). However even pythonw should have its console output redirected to xlpython.log in the xlpython folder.

Don't know why pdb isn't working, will have to look into that and the other issues as soon as I have a spare moment.

Cheers

Eric

EricE commented 10 years ago

Hi Eric, The window did appear, but nothing showed up in it from the print statements. Just tried again and print does go to the log which should get me through the weekend!

Thanks, Eric

ericremoreynolds commented 10 years ago

Did you try commenting the RedirectOutput line (put a # at the beginning of the line)?

EricE commented 10 years ago

Hi Eric, Oops, thought that was just related to having the output window show up. Now it works :)

Thanks, Eric

ericremoreynolds commented 10 years ago

Excellent :)