question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

question about pyimport, numpy, and implementation details

See original GitHub issue

Thanks for creating this package. It looks really neat.

I was particularly intrigued by the pyimport function you mentioned in the README. Having worked with some other javascript to python translators, I was surprised to see this work. Not only that it even works on something like the os module! I guess what you are doing is not translating targets of pyimport into javascript but marking them as untranslated python when you translate javascript to python.

Is that right?

That brings me to the question of numpy. When I try and use pyimport with numpy in the following snippet

import js2py
js2py.eval_js('pyimport numpy;\nconsole.log(numpy.mean([1,2,3,4]));')

it fails and I get an exception.

Digging into the exception a bit suggests that numpy.mean is trying to look at the type of its input and dispatch on that. But since the type of [1, 2, 3, 4] is a javascript object, numpy gets confused and dies. Various other attempts (including using EvalJs to put numpy.array and list into javascript) failed.

Any tips on how one could use js2py with numpy, pandas, etc.?

Thanks.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
PiotrDabkowskicommented, Jan 4, 2019

Hi, thanks, the pyimport is actually quite easy as the Js gets translated to Py and the python modules do not need translation at all, the translated code simply calls python functions directly (with some simple wrapping of arguments and the return value). This simple wrapping is exactly the reason why your np.mean function does not work. As you can see in the conversion table in readme, the Js types are converted to Py types as follows:

Boolean -> bool
String -> unicode (str in Python 3)
Number -> float (or int/long if whole number)
undefined -> None
null -> None
OTHER -> JsObjectWrapper

Hence the value received by the numpy.mean is actually a JsObjectWrapper, the reason why this is done this way can become clearer after checking the readme file. I have just added a hacky opt-in config to convert js arrays and objects to python lists and dicts implicitly:

>>> import js2py
>>> js2py.base.PyJs.CONVERT_TO_PY_PRIMITIVES = True
>>> js2py.eval_js('pyimport numpy;var np=numpy;var a = np.array([1,2,3]);var b = np.array([5,1,3]);a.__add__(b)')
True
array([6, 3, 6])
0reactions
abaltercommented, Jun 16, 2019

Nice, but no datatables or stats 😃

I see what you mean about executing JS by Python. I’m probably thinking in reverse of what makes the most sense. It would probably be better to create an API to the python libraries I want in Python and then call them from node via https://github.com/extrabacon/python-shell or something like it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

pyimport numpy fails · Issue #65 · JuliaPy/PyCall.jl
Hello, I use Ubuntu 12.04, 64 bit. I installed numpy 1.7.1 with pip. Importing numpy fails: julia> using PyCall julia> @pyimport numpy ERROR:...
Read more >
NumPy: the absolute basics for beginners
NumPy (Numerical Python) is an open source Python library that's used in almost ... An array is a grid of values and it...
Read more >
PyCall import("numpy") produces MKL FATAL ERROR
executable in python and the desired conda enviorment). I have a python library "mylib" that uses numpy. Problem. When I tried to pyimport(" ......
Read more >
Please use the following python code and subsequent txt ...
log_reg_gradient_descent.py import numpy as np import matplotlib.pyplot as plt ... code and subsequent txt data to answer the above question if possible.
Read more >
Python import: Advanced Techniques and Tips
Technical Detail: The module namespace is implemented as a Python dictionary and ... third_party/serializers/json.py import json class JsonSerializer: def ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found