extremeheat / JSPyBridge

🌉. Bridge to interoperate Node.js and Python
MIT License
690 stars 53 forks source link

Local python module not loadable via javascript #59

Open TA2k opened 2 years ago

TA2k commented 2 years ago

I copied a python module in my folder and not installed via pip With a python script this import ist working but not via pythonia in javasript

Folder:

module_name
     __init__.py
    code.py
python.py
javascript.js

python.py:

from module_name import connect
connection = connect(
    account =user,
    password=pass,
)

On javascript this is not working javasript.js

import { python } from "pythonia";
const module_name = await python("module_name");

const connection = await module_name.connect({
    account: "mail",
    password: "pass",
});
python.exit(); // Make sure to exit Python in the end to allow node to exit. You can also use process.exit.
... across the bridge ...

> return _bootstrap._gcd_import(name[level:], package, level)
  at import_module (/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py:127)
  at _gcd_import (<frozen importlib._bootstrap>:1014)
  at _find_and_load (<frozen importlib._bootstrap>:991)
  at _find_and_load_unlocked (<frozen importlib._bootstrap>:973)
🌉 ModuleNotFoundError: No module named 'module_name']
 % Exception ignored in: <function Proxy.__del__ at 0x7fb21a10dca0>
Traceback (most recent call last):
  File "node_modules/pythonia/src/pythonia/proxy.py", line 226, in __del__
  File "node_modules/pythonia/src/pythonia/proxy.py", line 111, in free
  File "node_modules/pythonia/src/pythonia/Bridge.py", line 273, in queue_request
  File "node_modules/pythonia/src/pythonia/interface.py", line 19, in queue
SystemExit: 1

Used module: https://github.com/nbogojevic/midea-beautiful-air/tree/main/midea_beautiful

austincondict-AngelOak commented 2 years ago

Looks to be just a python error regarding your project structure. Does that python.py script of yours actually run successfully if you launch it on its own? Because I don't believe it would unless you've configured the module_name/__init__.py file to correctly expose your API functions (i.e. connect()). It should work if your __init__.py file looks something like this:

from .code import connect
# import other functions to expose

__all__ = [
    "connect",
    # names of all imported functions to be exposed
]

Don't believe this is a pythonia issue.

TA2k commented 2 years ago

It is working with python3 python.py It looking like this

__all__ = (
   "connect"

)
def connect( )....

Example: https://github.com/nbogojevic/midea-beautiful-air/blob/8472bd025831672d3bae01be5ac60bbf77b6d03e/midea_beautiful/__init__.py#L23

You can try it by yourself, for example with the space.js example remove pygame via pip and install pygame locally

pip3 install pygame -t .

and rerun space.js

pip3 install pygame

is working

TA2k commented 1 year ago

@extremeheat do you have an idea to import local installed modules?

hadifikri commented 1 year ago

Got the same problem. Solve it by including the search path manually:

const sys = await python('sys');
await sys.path.insert(0,'.');
const module_name = await python('module_name')