Project

Profile

Help

Difficulty installing Saxon/C in Python project

Added by Keith Miner over 4 years ago

While I have used XSLT/X-Path for years I am very new to. Since I am anticipating projects at work that will involve Python and XML it seemed like a good idea to start learning Saxon/C with Python. So far, I have succeeded in installing Saxon-HE 9.9.1.5J, Cython (current version using PIP), and Saxon/C 1.2.0 on my system (MacOS 10.15.1). I was also able to run the sample files that came with Saxon/C.

My problem is that while I can run the samples fine (using PyCharm) I can’t run a very simple file that imports saxonc in other PyCharm python projects. The error is “ModuleNotFoundError: No module named 'saxonc'” I confirmed that Cython was installed in that environment (by try to install it again using PIP), but (as you know) PIP is not available for Saxon/C. I suspect the problem is with how the venv virtual environment is set up.

To troubleshoot I got my test file to run by copying in saxonc.pyx, saxonc.cpython-37m-darwin.so, nodekind.py into the project directory, but even then PyCharm warns that “No module named saxonc.” It does not give this warning in the sample files. Just dropping in files seems like a hack to me but I can’t figure out the “right” way to install Saxon/C into a Python project.

Any advice?

Thanks


Replies (6)

Please register to reply

RE: Difficulty installing Saxon/C in Python project - Added by Martin Honnen over 4 years ago

For import saxoncto work, you need to set the PYTHONPATH to include the directory where the saxonc module is. You can do that in your shell or from within Python, see https://github.com/hou2zi0/xslt-simple-text-processing-toolbox#using-xsl-transformation-with-saxonc-in-python for the latter doingsys.path.append("/path/to/saxonc")`.

RE: Difficulty installing Saxon/C in Python project - Added by O'Neil Delpratt over 4 years ago

Thanks Martin. I will try to add some of the details from the webpage you mentioned to our documentation.

I have never used the PyCharm IDE but I will try to look into it.

RE: Difficulty installing Saxon/C in Python project - Added by Keith Miner over 4 years ago

Thanks Martin! With your advice (and ideas from the page you linked to) I was able to run the file in my project without the file-copy hack. I hate to sound greedy, but there is one challenge left. The PyCharm IDE still warns that there is no module named saxonc, even though the file runs. I suspect that this is because I appended the path in code as suggested:

import sys
sys.path.append("/Users/minermac/dev/libsaxon-HEC-mac-setup-v1.2.1/Saxon.C.API/python-saxon")
import saxonc

FYI, I added the path to python-saxon to the environmental variables PATH and PYTHONPATH. I also added JET_HOME=/usr/local/lib/rt. I was unsuccessful adding DYLD_LIBRARY_PATH as recommended on that other page. For some reason that just wouldn't save.

The reason why this would be helpful to fix is that the IDE does a nice job of code completion when it can find the module.

Thanks again

RE: Difficulty installing Saxon/C in Python project - Added by Martin Honnen over 4 years ago

I understand what you want to achieve but as I am not familiar with that IDE and also not an expert on how Python in general finds modules, other than having tried to find a way for me on Windows to have my code find saxonc based on https://docs.python.org/3/tutorial/modules.html#the-module-search-path I am afraid I am sorry, but I don't know how to get that IDE to know about the module location. I would suggest to ask in forum of other users of that IDE on how to set it up or on how to set up a venv environment when a Python module has not been installed through PIP but you just know its folder or directory on your system.

RE: Difficulty installing Saxon/C in Python project - Added by O'Neil Delpratt over 4 years ago

I have managed to do some experiments using Spyder in Anaconda. As mentioned above the code completion works fine with Saxon/C. To run the Python scripts more that once I had to change from using the with statement with PySaxonProcessor to the following:

proc = saxonc.PySaxonProcessor(license=False)

This is to avoid the release function being called. Otherwise the kernel will have to be restarted each time. When exiting the Spyder IDE I suggest calling running a python script with the code proc.release() to safely release the JET VM GC threads.

RE: Difficulty installing Saxon/C in Python project - Added by Keith Miner over 4 years ago

Thanks Martin for getting back to me on this. If I find a solution I will post it here. At least I have a way forward now.

    (1-6/6)

    Please register to reply