GithubHelp home page GithubHelp logo

Comments (8)

yglukhov avatar yglukhov commented on June 5, 2024 1

You can specify path to libpython using nimpy.py_lib.pyInitLibPath. Also see #171.

from nimpy.

dmknght avatar dmknght commented on June 5, 2024 1

So here's the working code of mine, Nim 1.6.0, nimpy 0.2.0

import nimpy

let
  py = pyBuiltinsModule()

echo py.eval("2+1", py.dict())

Solution found at https://blogs.mathworks.com/loren/2020/03/03/matlab-speaks-python/

from nimpy.

yglukhov avatar yglukhov commented on June 5, 2024

What does echo pyImport("sys").paths say?

from nimpy.

pietroppeter avatar pietroppeter commented on June 5, 2024

that one:

/Users/pietroppeter/.nimble/pkgs/nimpy-0.2.0/nimpy.nim(867) getAttr
/Users/pietroppeter/.nimble/pkgs/nimpy-0.2.0/nimpy/py_utils.nim(98) raisePythonError
Error: unhandled exception: <type 'exceptions.AttributeError'>: 'module' object has no attribute 'paths' [Exception]

but I guess it might have been path and this goes with Python 2.7!

['/usr/lib/python27.zip', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-darwin', '/usr/lib/python2.7/plat-mac', '/usr/lib/python2.7/plat-mac/lib-scriptpackages', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']

I guess this is it, how do I fix it?
the Mac came with python 2.7 for python but I did change the alias to have it use python3

from nimpy.

pietroppeter avatar pietroppeter commented on June 5, 2024

tried both paths below (the first is the one given me by find_libpython, the second it looked reasonable to me by looking around in the folders):

import nimpy
import nimpy / py_lib


#pyInitLibPath "/usr/local/Cellar/[email protected]/3.9.10/Frameworks/Python.framework/Versions/3.9/Python"
pyInitLibPath "/usr/local/Cellar/[email protected]/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/libpython3.9.dylib"

let py = pyBuiltinsModule()
discard py.exec("sum([1, 2, 3])")

but the result is always an error of loading like this

Error: unhandled exception: Could not load libpython. Tried /usr/local/Cellar/[email protected]/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/libpython3.9.dylib

not sure what I should use...

from nimpy.

yglukhov avatar yglukhov commented on June 5, 2024

Not sure, could it be because of architecture mismatch?

file /usr/local/Cellar/[email protected]/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/libpython3.9.dylib
file $MY_EXECUTABLE

from nimpy.

pietroppeter avatar pietroppeter commented on June 5, 2024

I went back this (sorry for the delay) and I think back then I had indeed an architecture mismatch (default python on Mac M1 is x86_64). I installed an arm python and now I have both my new python and my nim executable that are both arm (checked with file see below).

I do get a different error now. For reference now I compile and run:

import nimpy
import nimpy / py_lib

pyInitLibPath "/Library/Frameworks/Python.framework/Versions/3.11/lib/libpython3.11.dylib"

let py = pyBuiltinsModule()
discard py.exec("sum([1, 2, 3])")

and the error is:

/Users/pietroppeter/play_nimpy/nimpyissue261.nim(10) nimpyissue261
/Users/pietroppeter/.nimble/pkgs/nimpy-0.2.0/nimpy.nim(845) callMethodAux
/Users/pietroppeter/.nimble/pkgs/nimpy-0.2.0/nimpy/py_utils.nim(98) raisePythonError
Error: unhandled exception: <class 'SystemError'>: frame does not exist [Exception]

checking architecture with file:

❯ file /Library/Frameworks/Python.framework/Versions/3.11/lib/libpython3.11.dylib
/Library/Frameworks/Python.framework/Versions/3.11/lib/libpython3.11.dylib: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit dynamically linked shared library x86_64] [arm64]
/Library/Frameworks/Python.framework/Versions/3.11/lib/libpython3.11.dylib (for architecture x86_64):    Mach-O 64-bit dynamically linked shared library x86_64
/Library/Frameworks/Python.framework/Versions/3.11/lib/libpython3.11.dylib (for architecture arm64):     Mach-O 64-bit dynamically linked shared library arm64

❯ file nimpyissue261
nimpyissue261: Mach-O 64-bit executable arm64

from nimpy.

dmknght avatar dmknght commented on June 5, 2024

I'm having the same issue here. I used the find_libpython and added pyInitLibPath. My test code:

import nimpy
import nimpy / py_lib


pyInitLibPath "/usr/lib/x86_64-linux-gnu/libpython3.11.so.1.0"
let py = pyBuiltinsModule()
discard py.exec("sum([1, 2, 3])")

Output:

/tmp/test.nim(8)         test
/home/dmknght/.nimble/pkgs/nimpy-0.2.0/nimpy.nim(850) callMethodAux
/home/dmknght/.nimble/pkgs/nimpy-0.2.0/nimpy/py_utils.nim(98) raisePythonError
Error: unhandled exception: <class 'SystemError'>: frame does not exist [Exception]
Error: execution of an external program failed: '/tmp/test '

I don't really think this problem comes from different architecture

Update: To make sure pyBuiltinsModule() worked normally, i tried echo py.bin(3) to test and ofc it worked. I tried digging a bit and I found that code use pybind could have this problem too
https://stackoverflow.com/questions/53115558/compile-and-execute-ast-using-pybind11-or-python-c-api

Update 2: According to the stackoverflow link, apparently we have to pass py.globals() into eval or exec. However, it showed the NULL exception (just like in the comments). I didn't find a way to execute code properly. But here's my code

import nimpy

let
  py = pyBuiltinsModule()
  modl_builtin = pyImport("builtins")


echo py.eval("x+1", py.globals())
echo modl_builtin.exec("x + 1", py.globals())

from nimpy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.