Comments (15)
I just tried on a vanilla Ubuntu 20.04 container and this worked for me (can't think of anything that shouldn't also be working on WSL):
docker run --name ub1 --hostname ub1 -ti ubuntu:20.04
and in the Ubuntu 20.04 bash shell run (all as root):
apt update
apt install wget vim
apt install git #for go get/install
apt install build-essential #gcc for cgo
apt install pkg-config #for cgo to find python includes
apt install software-properties-common #for add-apt-repository
# install latest Go (distro packages are outdated)
cd $HOME
mkdir Downloads
cd Download
wget https://go.dev/dl/go1.19.3.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.19.3.linux-amd64.tar.gz
ln -s /usr/local/go/bin/go /usr/local/bin/go
# install py 3.7 from ppa (as no distro packages for Py 3.7)
add-apt-repository ppa:deadsnakes/ppa
apt install python3.7 python3.7-dev
# we explicitly need a "python3.pc" (not "python-3.7m.pc" or so)!
cp /usr/lib/x86_64-linux-gnu/pkgconfig/python-3.7m.pc /usr/lib/x86_64-linux-gnu/pkgconfig/python3.pc
# at this point a "pkg-config --list-all" should now show a record exactly named "python3"
# create a Go test program that uses go-python/cpy3:
cd $HOME
mkdir hello
cd hello
go mod init hello
vi main.go
contents of main go:
package main
import "github.com/go-python/cpy3"
func main() {
defer python3.Py_Finalize()
python3.Py_Initialize()
python3.PyRun_SimpleString("print('hello from python in go')")
}
and back in the Ubuntu shell:
#this adds the go-python/cpy3 dep to go.mod and downloads it
go mod tidy
Now run the program:
go run .
Result:
$ root@ub1:~/hello# go run .
hello from python in go
from cpy3.
what Python version are you using? This library currently only works with Python 3.7. This error usually happens when trying to use a newer version of Python.
It is possible to use newer version of Python if you remove some functions from go-python/cpy3
/ datadog/go-python3
(bindings to stuff that has been removed from Python's C-API in newer version). For example, Python 3.8 works if you remove the bindings for PyEval_ReInitThreads
, like I did here. More stuff might need to get removed for Python 3.9 and newer.
from cpy3.
So when I run 'python --version' in the terminal, it outputs 'Python 3.7.15.' Do I need to be on an earlier version?
How would I delete the bindings. Should I download the zip of that commit or should I edit the import?
from cpy3.
what does pkg-config --modversion python3
say?
from cpy3.
It returns 3.8 even though version is set to 3.7 and includedir to the python3.7m folder/directory that contains the Python.h file.
from cpy3.
not sure why that is but you can likely get it to work by:
- create a folder with a copy of the correct
python3.pc
file - explicitly set env var
PKG_CONFIG_PATH
to this path (i.e.export PKG_CONFIG_PATH=/path/to/my-dir
make sure the env var is set when you install this Go module and when you run go build
.
(also there's a chance that you might already have a "bad" binary in your Go cache from the previous installation attempt, here go clean -cache
and go clean -modcache
should help).
from cpy3.
btw what's your OS + OS version and how did you install Python 3.7?
from cpy3.
So I edited the PKG_CONFIG_PATH
var, I hadn't specified the pkg-config
folder but just the directory it is in. Running pkg-config --modversion python3
now returns 3.7 as intended. After running the go clean -cache
and go clean -modcache
commands go build
returns this error instead (which I have encountered before but thought I had solved by specifiying the includedir
in the python3.pc
file):
../../go/pkg/mod/github.com/go-python/[email protected]/boolean.go:11:10: fatal error: Python.h: No such file or directory
And I'm running it on a WSL Ubuntu 20.04 and I believe I had installed Python using the command sudo apt install python3.7
.
from cpy3.
one more note: Instead of installing from the ppa deadsnakes repo you could also build Python 3.7 from source. Here's an example on Debian, it should be very similar on Ubuntu:
#18 (comment)
from cpy3.
Quick question, is there an advantage to building Python 3.7 from source rather than using deadsnakes?
from cpy3.
technically probably not much difference. (More control and you don’t have to trust a third party package repo).
from cpy3.
I'm setting up the docker container as you did, to as closely as possible trace your steps, and with the command cp /usr/lib/x86_64-linux-gnu/pkgconfig/python-3.7m.pc /usr/lib/x86_64-linux-gnu/pkgconfig/python3.pc
, I am getting the error cp: cannot stat '/usr/lib/x86_64-linux-gnu/pkgconfig/python3.7m.pc': No such file or directory
.
from cpy3.
did you install python3.7-dev
?
from cpy3.
I found the issue, it was a stupid mistake in the directory that I missed. Continued with the rest of the commands and it worked!
Thank you so much for your help and time, I appreciate it so much!
from cpy3.
glad it works :)
from cpy3.
Related Issues (18)
- add support for multiple Python3 versions (not just 3.7) HOT 14
- buffer protocol HOT 5
- review tests on Windows HOT 2
- move CI to github actions
- Go module
- Let's try it, python bindings cgo-less
- Convert go struct to dict HOT 6
- python function call failed HOT 2
- how can i call python3 function in groutine HOT 2
- repo example codes failed to run HOT 3
- fedora and python 3.11.1: cannot compile HOT 2
- Pass arguments to a python script HOT 3
- Possibility to use Py_Initialize only once and use an external python script from multiple go routines HOT 2
- add goroutines example HOT 1
- go get for go-python3 on mac M1 is not working. HOT 11
- installation problem HOT 5
- Memory Leaks while running Concurrent Go Routines with a Python File. HOT 13
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 cpy3.