hmenyus / node-calls-python

Call Python from NodeJS directly in-process without spawning processes
MIT License
252 stars 26 forks source link

Python Interpreter from venv #51

Closed RonaldErnst closed 1 year ago

RonaldErnst commented 1 year ago

I saw that the python interpreter that is used to run scripts is loaded from the PATH variable. Because of that our virtual environment is not being used and therefore no imports work.

Is it possible to somehow add the interpreter path that should be used?

hmenyus commented 1 year ago

you can use addImportPath function to do that.

RonaldErnst commented 1 year ago

I'm sorry if this is too much to ask for but since there is no documentation for that function could you provide me with an example?

I'm currently on Windows and neither py.addImportPath("path/to/.venv/Scripts/python.exe"); nor py.addImportPath("path/to/.venv/Scripts"); seem to work for me.

Am I doing something wrong? Could you provide me with some documentation for the usage?

I would really prefer using your library over child_process

hmenyus commented 1 year ago

This may work: py.addImportPath("path/to/.venv/Lib/site-packages")

Python uses the sys.path variable for seach paths of the models. addImportPath will add the directory you specify to the begining of sys.path. So you have to use the directory where your models are located (in case of venv that is Lib/site-packages).

RonaldErnst commented 1 year ago

Thank you that worked!