Comments (9)
what exactly did you try?
could you post a simple reproducer of this issue?
(also, do note that sbinet/go-python
only support CPython-2, not CPython-3)
from go-python.
what exactly did you try?
could you post a simple reproducer of this issue?
(also, do note thatsbinet/go-python
only support CPython-2, not CPython-3)
go1.16.4
python 2.7.12
The Cpython version is Cpython2, and it is ok to call python2 programs via GO to return integers directly. Here is the code you can take a look at
- go
package main
import (
"github.com/sbinet/go-python"
"fmt"
)
var (
pyLogModule *python.PyObject
pyTest *python.PyObject
)
func init() {
err := python.Initialize()
if err != nil {
panic(err.Error())
}
}
func main(){
pyInit("creditcard","creditcard1",0)
}
func pyInit(datasetName string, dataFile string, epsilon float64) {
m := python.PyImport_ImportModule("sys")
sysPath := m.GetAttrString("path")
python.PyList_Insert(sysPath, 0, python.PyString_FromString("/home/zhaoxinbo/Biscotti/ML/code"))
fmt.Println(sysPath)
pyLogModule = python.PyImport_ImportModule("logistic_model2")
pyTest = pyLogModule.GetAttrString("test2")
if pyTest == nil {
fmt.Println("Error importing function")
}
pyNumFeatures := pyTest.CallFunction(python.PyString_FromString(datasetName))
numFeatures := python.PyInt_AsLong(pyNumFeatures)
fmt.Println(numFeatures) }
- python
import pandas
def test2(dataset_name ):
#return 256
return int(pd.read_csv("../data/creditcard.csv").shape[1])
from go-python.
is that this exact python code?
because you import pandas
as pandas
and try to use it as pd
. (-1
sounds like a typical CPython-2 error value that one might get on the C side when e.g. an import fails)
from go-python.
is that this exact python code?
because you importpandas
aspandas
and try to use it aspd
. (-1
sounds like a typical CPython-2 error value that one might get on the C side when e.g. an import fails)
——————————————————————————————————————
Sorry, python source code is import Pandas as pd, accidentally deleted when copying up. I wonder if it is caused by the incompatibility between language versions or the incompatibility of Pandas package
from go-python.
is that this exact python code?
because you importpandas
aspandas
and try to use it aspd
. (-1
sounds like a typical CPython-2 error value that one might get on the C side when e.g. an import fails)
Maybe you can help me run the above code to test whether the same error message will appear. If GO-Python can be used normally, my current project will save a lot of effort, really bother you
from go-python.
hi,
with the following code:
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/sbinet/go-python"
)
var (
pyLogModule *python.PyObject
pyTest *python.PyObject
)
func init() {
err := python.Initialize()
if err != nil {
panic(err.Error())
}
}
func main() {
run()
}
func run() {
dir, err := os.MkdirTemp("", "go-python-")
if err != nil {
panic(fmt.Errorf("could not create temp dir: %+v", err))
}
defer os.RemoveAll(dir)
fname := filepath.Join(dir, "data.csv")
err = os.WriteFile(fname, []byte(data), 0644)
if err != nil {
panic(fmt.Errorf("could not create data file: %+v", err))
}
m := python.PyImport_ImportModule("sys")
sysPath := m.GetAttrString("path")
python.PyList_Insert(sysPath, 0, python.PyString_FromString(dir))
fmt.Println(sysPath)
err = os.WriteFile(filepath.Join(dir, "m.py"), []byte(module), 0644)
if err != nil {
panic(fmt.Errorf("could not create pandas module: %+v", err))
}
pyLogModule = python.PyImport_ImportModule("m")
if pyLogModule == nil {
panic(fmt.Errorf("could not import module"))
}
pyTest = pyLogModule.GetAttrString("test")
if pyTest == nil {
fmt.Println("Error importing function")
panic(fmt.Errorf("could not import function"))
}
pyNumFeatures := pyTest.CallFunction(python.PyString_FromString(fname))
if pyNumFeatures == nil {
exc, val, tb := python.PyErr_Fetch()
fmt.Printf("exc=%v\nval=%v\ntb=%v\n", exc, val, tb)
panic(fmt.Errorf("could not call function"))
}
numFeatures := python.PyInt_AsLong(pyNumFeatures)
fmt.Println(numFeatures)
}
const module = `
import pandas as pd
def test(fname):
print("fname: %s, pandas: %s" % (fname, pd.__file__))
return int(pd.read_csv(fname).shape[1])
`
const data = `1,2,3,4,hello
5,6,7,8,world
`
in an ubuntu container (docker run --rm -it ubuntu:18.04
), I get:
$> go run ./main.go
['/tmp/go-python-3822668890', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
fname: /tmp/go-python-3822668890/data.csv, pandas: /usr/lib/python2.7/dist-packages/pandas/__init__.pyc
5
from go-python.
hi,
with the following code:
package main import ( "fmt" "os" "path/filepath" "github.com/sbinet/go-python" ) var ( pyLogModule *python.PyObject pyTest *python.PyObject ) func init() { err := python.Initialize() if err != nil { panic(err.Error()) } } func main() { run() } func run() { dir, err := os.MkdirTemp("", "go-python-") if err != nil { panic(fmt.Errorf("could not create temp dir: %+v", err)) } defer os.RemoveAll(dir) fname := filepath.Join(dir, "data.csv") err = os.WriteFile(fname, []byte(data), 0644) if err != nil { panic(fmt.Errorf("could not create data file: %+v", err)) } m := python.PyImport_ImportModule("sys") sysPath := m.GetAttrString("path") python.PyList_Insert(sysPath, 0, python.PyString_FromString(dir)) fmt.Println(sysPath) err = os.WriteFile(filepath.Join(dir, "m.py"), []byte(module), 0644) if err != nil { panic(fmt.Errorf("could not create pandas module: %+v", err)) } pyLogModule = python.PyImport_ImportModule("m") if pyLogModule == nil { panic(fmt.Errorf("could not import module")) } pyTest = pyLogModule.GetAttrString("test") if pyTest == nil { fmt.Println("Error importing function") panic(fmt.Errorf("could not import function")) } pyNumFeatures := pyTest.CallFunction(python.PyString_FromString(fname)) if pyNumFeatures == nil { exc, val, tb := python.PyErr_Fetch() fmt.Printf("exc=%v\nval=%v\ntb=%v\n", exc, val, tb) panic(fmt.Errorf("could not call function")) } numFeatures := python.PyInt_AsLong(pyNumFeatures) fmt.Println(numFeatures) } const module = ` import pandas as pd def test(fname): print("fname: %s, pandas: %s" % (fname, pd.__file__)) return int(pd.read_csv(fname).shape[1]) ` const data = `1,2,3,4,hello 5,6,7,8,world `in an ubuntu container (
docker run --rm -it ubuntu:18.04
), I get:$> go run ./main.go ['/tmp/go-python-3822668890', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages'] fname: /tmp/go-python-3822668890/data.csv, pandas: /usr/lib/python2.7/dist-packages/pandas/__init__.pyc 5
Thanks a lot. I got the same result on my server. I will try to integrate it into the project tomorrow.
from go-python.
My biggest mistake was the path to open the file in Python: I used a relative path. If you need to use a relative path, it should be relative to the go file or gobin file, not relative to the Python file. This is why Python files can be executed on their own, while go calls cannot.
from go-python.
let me interpret this as a closed issue.
feel free to reopen otherwise.
from go-python.
Related Issues (20)
- Does it works on Centos7 i686 system? HOT 1
- go get report errors HOT 4
- panic when register callback from go into python as module HOT 1
- How to call a function of Go from python HOT 3
- After running go-python script for a while, the memory becomes larger and larger HOT 1
- How to build the environment required by go-golang under window
- panic when run multi goroutine HOT 3
- Error reporting for execute command under win10 HOT 3
- Hi,
- Question: Usable with WebAssembly (WASM)? HOT 1
- Documentation not displayed due to license restrictions. HOT 2
- D:\go\project\src\github.com\sbinet\go-python\capi.go:33:32: could not determine kind of name for C.METH_OLDARGS
- Exception SystemError: '/builddir/build/BUILD/Python-2.7.5/Objects/classobject.c:521: bad argument to internal function'
- can i import python module like selenium to use inside golang code?
- Let's try it, python bindings cgo-less HOT 1
- Cannot pass Integer type to CallFunction
- go panic when multi thread HOT 1
- How to use cPickle loads or dump complicated data, like array, class object
- memory leak? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-python.