Todd Vanyo
Todd Vanyo

Reputation: 593

python xlwings in virtualenv possible?

I've tried to start playing with xlwings using python 3.4.3 in a virtualenv, but one of the examples programs errors out because it cannot see numpy, which is very much installed in the virtualenv. pip freeze run in the virtualenv shows (cleaned some of the obviously non-essential out):

appscript==1.0.1
lxml==3.4.4
numpy==1.9.2
pandas==0.16.1
psutil==3.0.1
ptyprocess==0.5
pyparsing==2.0.3
python-dateutil==2.4.2
virtualenv==13.0.3
virtualenv-clone==0.2.5
virtualenvwrapper==4.6.0
xlrd==0.9.3
XlsxWriter==0.7.3
xlwings==0.3.5

I'm not sure that setting PYTHON_MAC to the location of my 3.4.3 install (done via Homebrew) is going to solve this because the location of the site-packages is elsewhere.

Is it possible to run xlwings from a virtualenv or do I need to have my desired packages installed in the system wide site-packages as well?

Upvotes: 2

Views: 3398

Answers (5)

Shaheed ulHaq
Shaheed ulHaq

Reputation: 516

Just one Interpreter property in the xlwings.conf sheet did the trick for me.

Config Sheet

Upvotes: 0

Ashish Jain
Ashish Jain

Reputation: 467

I faced the exception "Exception: Workbook.caller() must not be called directly. Call through Excel or set a mock caller first with Book.set_mock_caller()." while trying to work with XLWings in a conda environment and this issue is not addressed yet. So here is the solution, first launch Excel from conda env like this:

(env_for_python_36) C:\Users\ashish.jain\Desktop>"C:\Program Files\Microsoft Office\Office16\EXCEL.exe"

Then, in the Python file where you load the Excel sheet, write code as follows:

xlsm_path = r'C:\Users\ashish.jain\Desktop\Data.xlsm'
xlsm_path = xlsm_path.replace('\\', '/')
wb = xw.Book(xlsm_path)

Upvotes: 0

Dylan Hogg
Dylan Hogg

Reputation: 3398

Launching Excel from an activated virtual environment (with appropriately installed packages) will provide the correct environment for xlwings.

An example for a Windows command prompt:

C:\code\xlwings>.\venv\Scripts\activate
(venv) C:\code\xlwings> "C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE"

Then open your xlwings XLSM file and run.

Upvotes: 2

Felix Zumstein
Felix Zumstein

Reputation: 7070

You need to set the location of PYTHON_MAC (or PYTHON_WIN) to the location of your virtualenv. E.g. PYTHON_MAC = ".../env/bin/python".

Upvotes: 6

PHC
PHC

Reputation: 171

Hi solved a similar problem changing the PYTHON_WIN variable as @Felix Zumstein suggested, but in the xlwings VBA module that's imported into your excel workbook via the xlwings.base file.

My issue trying to run xlwings with python3 setup using the Anaconda package in an anaconda environment.

When I tried running the demo described at the xlwings quickstart page http://xlwings.org/quickstart/ I got the following error

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\PHCostello\Documents\UbuntuHome\Projects\XLWings\mymodule.py", line 6, in rand_numbers
wb = Workbook.caller()  # Creates a reference to the calling Excel file
AttributeError: type object 'Workbook' has no attribute 'caller'

I had an old python2 with old xlwings setup in the standard environment, so that came as xlwings was not using the python3 setup in the anaconda environment.

I fixed by changing path the xlwings uses to call python, via going into the vba editor and selecting the xlwings VBA module imported into the Excel workbook. Note, this is the xlwings.bas file you import to the workbook, not a python module.

There's a function in there called Settings which has a variable called PYTHON_WIN that you can change to directory where your python.exe is for environment your using. When I changed everything worked fine.

This solution has advantage of only using this python path for this workbook, which is also a disadvantage I guess if you send your sheet to a third party where you'll want to remove the hardcoded path.

Upvotes: 1

Related Questions