Menu

Import problem

Eric
2014-08-11
2014-08-14
  • Eric

    Eric - 2014-08-11

    Hi Eric,
    Earlier this year I asked about using virtualenv with ExcelPython and you said you weren't very familiar with it. All my projects are setup using virtualenvs (really good thing!) so I went a different way at the time. I'd really like to use ExcelPython though so I'm making a push to try and make it work. The problem I'm having is when a module imports a library that is only in the virtualenv's site-packages directory.

    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)

    =================
    Sub test()
    Set m = Py.Module("aaaa", _
    Py.AddPath("..long_path...\virtenv\Lib\site-packages\requests"))
    MsgBox Py.Str(Py.Call(m, "DoubleSum", Py.Tuple(1, 2)))
    End Sub
    ==================

    I get a pywin32 error message on the Set m statement:
    http://i.imgur.com/tE9kseW.png

    I hoped by adding the path to the requests package that it would be found but I get the same error with or without Py.AddPath(...). Is there a way to bring in packages that aren't installed in the default site-packages directory?

    Secondary question, can you add multiple paths by using ";" as a separator in AddPath?

    Thanks,
    Eric

     
    • Eric Reynolds

      Eric Reynolds - 2014-08-11

      Hello Eric,

      V2 is currently undergoing active development so right now there are still
      quite a few things to smooth out.

      One of these is the error message display... could you please click 'debug'
      then type ?Err.Description in the immediate window to get the whole
      untruncated error message?

      And yes, you can ;-separate multiple paths.

      However, in v2 the correct way to approach your use case is to edit the
      xlpython.cfg file so that it runs the right python executable with the
      right environment/folders. I haven't got round to putting up some proper
      documentation explaining how to do this but I intend to in the next few
      days.

      Pls open up an issue on github so i don't forget to address your issue.

      Ciao

      Eric

       

      Last edit: Eric Reynolds 2014-08-12
    • Eric Reynolds

      Eric Reynolds - 2014-08-14

      Hi Eric,

      Did you try out what I wrote in the GitHub issue? Don't know if you got a notification that I replied.

      Regards, Eric.

       
  • Eric

    Eric - 2014-08-11

    Hi Eric,
    Here's the full error message:

    ?err.Description
    Unexpected Python Error: Traceback (most recent call last):
    File "D:\Python27\lib\site-packages\win32com\server\policy.py", line 277, in Invoke
    return self.invoke(dispid, lcid, wFlags, args)
    File "D:\Python27\lib\site-packages\win32com\server\policy.py", line 282, in invoke
    return S_OK, -1, self.invokeex(dispid, lcid, wFlags, args, None, None)
    File "D:\Python27\lib\site-packages\win32com\server\policy.py", line 585, in invokeex
    return func(*args)
    File "D:\SVNRepo\projects-sw\test\trunk\virtenv\src\xlpython\xlpyserver.py", line 76, in Module
    exec "import " + module in locals
    File "<string>", line 1, in <module>
    File "D:\SVNRepo\projects-sw\test\trunk\virtenv\src\aaaa.py", line 1, in <module>
    import requests
    ImportError: No module named requests

    and I've opened an issue.

    Thanks,
    Eric

     
  • Doug Jenkins

    Doug Jenkins - 2014-08-12

    I'll look forward to the details on the cfg file. I've been having problems with Ver. 2.01 with my Python modules not finding Numpy and Scipy. I finally fixed that by adding the full path to them to the Windows Path variable.

    The other problem I have had is that sometimes I get a message about being unable to open the log file. I have noticed that Python seems to be still running after quitting Excel. Could that be causing a problem with the log file?

     
    • Eric Reynolds

      Eric Reynolds - 2014-08-12

      Yes that's right, it is because of that. It is not yet clear to me why the
      Python process continues to run after quitting Excel, indeed it should not
      do that. However it in the GitHub issue list and hopefully it will be able
      to address it shortly.

       
    • Eric Reynolds

      Eric Reynolds - 2014-08-12

      Doug, you know what actually it's strange that you're having trouble loading the Numpy and Scipy modules.

      V2 differs from V1 in that it actually launches a python process, so everything should work exactly as it does when running python.exe from the command line.

      If you have the time and the will please could you give me the output of

      ?Py.Str(Py.GetAttr(Py.Module("sys"), "path"))
      

      compared with

      >>> sys.path
      

      run from your standard Python interpreter?

       

      Last edit: Eric Reynolds 2014-08-12
  • Doug Jenkins

    Doug Jenkins - 2014-08-13

    From the Python window I get: NameError: name 'sys' is not defined

    From the VBA Immediate Window I get:

    ['c:\Users\Doug\Documents\SPREAD\ExcelPython2\xlpython', 'C:\Users\Doug\Documents\Anaconda\lib\site-packages\openpyxl-1.8.5-py2.7.egg', 'c:\Users\Doug\Documents\SPREAD\ExcelPython2', 'C:\Users\Doug\Documents\Anaconda\python27.zip', 'C:\Users\Doug\Documents\Anaconda\DLLs', 'C:\Users\Doug\Documents\Anaconda\lib', 'C:\Users\Doug\Documents\Anaconda\lib\plat-win', 'C:\Users\Doug\Documents\Anaconda\lib\lib-tk', 'C:\Users\Doug\Documents\Anaconda', 'C:\Users\Doug\Documents\Anaconda\lib\site-packages', 'C:\Users\Doug\Documents\Anaconda\lib\site-packages\PIL', 'C:\Users\Doug\Documents\Anaconda\lib\site-packages\win32', 'C:\Users\Doug\Documents\Anaconda\lib\site-packages\win32\lib', 'C:\Users\Doug\Documents\Anaconda\lib\site-packages\Pythonwin', 'C:\Users\Doug\Documents\Anaconda\lib\site-packages\runipy-0.1.0-py2.7.egg', 'C:\Users\Doug\Documents\Anaconda\lib\site-packages\setuptools-3.6-py2.7.egg']

    The add-in is in:
    'c:\Users\Doug\Documents\SPREAD\ExcelPython2

     
    • Eric Reynolds

      Eric Reynolds - 2014-08-13

      Yes sorry you need to do import sys first.

       
  • Doug Jenkins

    Doug Jenkins - 2014-08-13

    OK, after import sys I get:

    ['', 'C:\Users\Doug\Documents\Anaconda\lib\site-packages\openpyxl-1.8.5-p
    y2.7.egg', 'C:\Users\Doug\Documents\Anaconda\python27.zip', 'C:\Users\Dou
    g\Documents\Anaconda\DLLs', 'C:\Users\Doug\Documents\Anaconda\lib', 'C:\ \Users\Doug\Documents\Anaconda\lib\plat-win', 'C:\Users\Doug\Documents\
    Anaconda\lib\lib-tk', 'C:\Users\Doug\Documents\Anaconda', 'C:\Users\Doug
    \Documents\Anaconda\lib\site-packages', 'C:\Users\Doug\Documents\Anacond
    a\lib\site-packages\PIL', 'C:\Users\Doug\Documents\Anaconda\lib\site-pa
    ckages\win32', 'C:\Users\Doug\Documents\Anaconda\lib\site-packages\win32
    \lib', 'C:\Users\Doug\Documents\Anaconda\lib\site-packages\Pythonwin', '
    C:\Users\Doug\Documents\Anaconda\lib\site-packages\runipy-0.1.0-py2.7.egg
    ', 'C:\Users\Doug\Documents\Anaconda\lib\site-packages\setuptools-3.6-py2
    .7.egg']

     
    • Eric Reynolds

      Eric Reynolds - 2014-08-14

      Ok so the mystery deepens because the Python search paths are
      identical (the one in Excel just contains a couple of extra paths
      which should hardly interfere) so it should find all the same
      libraries. The only slightly strange thing is C:\ \Users\Doug\Documents\Anaconda\lib\plat-win in your sys.path (note
      the space after C:\) however I think that's copy/paste artifact.

      If you can reproduce the error message we can look into it, however if
      it's now working I would suggest leaving it for now.

      By the way Doug, please note the new release of 2.0.2 on GitHub, I am
      also in the process of writing up some more docs which show how to use
      the add-in, which I believe from what I have seen of your work will
      probably interest you!

       
  • Doug Jenkins

    Doug Jenkins - 2014-08-14

    Thanks Eric.

    I'll go and have a look at the new files and docs.

     

Log in to post a comment.