extrabacon / python-shell

Run Python scripts from Node.js with simple (but efficient) inter-process communication through stdio
2.12k stars 224 forks source link

Unable to import module #299

Open DawChihLiou opened 1 year ago

DawChihLiou commented 1 year ago

The Question: Hi,

I'm trying out python-shell in a next.js project and I wasn't able to import my local python module in the script.

const messages = await PythonShell.runString(`from example_module import a, b, c`);

I'm using a pyproject.toml at the project root to define the local module:

[tool.poetry]
packages = [{include = "example_module", from = "python"}]

[tool.poetry.dependencies]
python = ">=3.11,<3.13"
bayesian = {path = "./python/example_module", develop = true }

This is the error I'm seeing.

- error TypeError [ERR_INVALID_ARG_TYPE]: The "string" argument must be of type string or an instance of Buffer or ArrayBuffer. Received an instance of PythonShellError
    at new NodeError (node:internal/errors:399:5)
    at Function.byteLength (node:buffer:793:11)
    at apiRes.end 
    at handler 
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  digest: undefined
}
PythonShellError: ModuleNotFoundError: No module named 'example_module'
    at PythonShell.parseError
    at terminateIfNeeded
    at ChildProcess.<anonymous>
    at ChildProcess.emit
    at ChildProcess._handle.onexit
    ----- Python Traceback -----
    File "/var/folders/rn/fvrs2khs7hq_hb99d7sqj_0h0000gn/T/pythonShellFile5599925788.py", line 2, in <module>
      from example_module import a, b, c{
  traceback: 'Traceback (most recent call last):\n' +
    '  File "/var/folders/rn/fvrs2khs7hq_hb99d7sqj_0h0000gn/T/pythonShellFile5599925788.py", line 2, in <module>\n' +
    '    from example_module import a, b, c\n' +
    "ModuleNotFoundError: No module named 'example_module'\n",
  executable: 'python3',
  options: null,
  script: '/var/folders/rn/fvrs2khs7hq_hb99d7sqj_0h0000gn/T/pythonShellFile5599925788.py',
  args: null,
  exitCode: 1,
  logs: []
}

Any suggestion is appreciated. Thank you so much.