How to use python package like langchain?

some can be used using

however some cannot, like langchain

is that because cpython engine in pyrevit is not compatible for some packages?

Hi @newshunhk ,
I’ve never tried it, but I see that langchain targets python 3.8.1+, so it should run fine if you switch your cpython engine to 3.8.5.

If you alredi use that version, does pyRevit output some kind of error?

It is already 3.8.5
it seems like pyrevit uses an special version of python even it said 3.8.5
I cant import ifcopenshell as well

Indeed it does! It leverages a library called in order to run python code inside .net process.

The cpython support is fundamentally broken, we hope to fix it in the next major version.

That being said, and if you really want to try wit the current version: what are the exact error messages it gives you? Can you show us the code you’re using? Only that way we can help you troubleshoot the issue.

as I want to import langchain
I checked langchain documentation, it supports 3.81 and up. Pyrevit is using cpython 3.85
for safe I used sys.path to append the package path too
I have tested other package like openai, all failed

Your output shows after line 6 that your package sys path is attach to python 3.10. Check that out.

From the python documentation the read_text method is available from version 3.5, so with the 3.8 engine it shouldn’t throw errors.
But it could be a bug, this happened to another user

I’ll investigate this

1 Like

if I don’t append the sys path, it will throw this:

No, adding your sys path to langchain is fine.
You need to set the PYTHONPATH properly so that it points to 3.8 while executing

Because right now you are using 3.8 in pyrevit but looking at packages installed for 3.10…

Holy smokes!
The problem is that, even though the compiled pathlib library is in the python 3.8.5 zip file, there’s also a file in the pyRevit-master\site-package folder that takes precedence!

I tried to remove the file, restart pyrevit and langchain imports fine!

This is definitely a bug, and brings up the broader question of how are we going to handle both python version 2 and 3 packages!


thank you so much!! should I remove that myself? or will have a updated pyrevit later?

The thing is, that file is (probably) used by other scripts that use IronPython, so it’s not safe to remove it completely. I think that a safe solution will not be quick to develop, but we’ll try :wink:

For the time being, you could try to rename the file to something else (like pathlib.txt) and rename it back if something breaks.

1 Like

cool! now I can run the package without error! :grinning:
by the way, is that anyway that I can let user to install the package by running the code?
in grasshopper I can put:
#r: pandas
and user will install the package when running it at first time

Unfortunately this is not possible, again it is something we’ll need to think about for the next major release…

The closes thing you can do to share the package is to create a lib extension, search for that term here and you’ll find discussion about that (I’m in a hurry and can’t do that for you, sorry :sweat_smile: )

1 Like