Comments (6)
hi @YangChuanMakeSense ,
for building python did you use configure --enable-shared
? (this gives a working <prefix>/lib/pkgconfig/python3.pc
) I just tried on Debian 9 and this Dockerfile
works for me:
I've split this into three build steps. The first one builds python from src, the second one builds the go program against our custom python and the third one is for running the compiled Go program (a more minimal container with no build tools installed and both our go program and our custom python in /opt
). You could of course also all do it in one container or machine.
# temp container to build python from source
FROM docker.io/library/debian:9 as builder-python
RUN apt-get -y update && apt-get -y install curl build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev
RUN curl https://www.python.org/ftp/python/3.7.10/Python-3.7.10.tgz -o python.tgz && tar xf python.tgz
RUN cd Python-3.7.10 && ./configure --enable-shared --prefix=/opt/py
RUN cd Python-3.7.10 && make -j `nproc`
RUN cd Python-3.7.10 && make install
# temp container to build the go program
FROM docker.io/library/debian:9 as builder-golang
COPY --from=builder-python /opt/py /opt/py
RUN echo /opt/py/lib > /etc/ld.so.conf.d/py.conf && ldconfig
RUN apt-get -y update && apt-get -y install git curl pkg-config gcc
RUN curl -L https://golang.org/dl/go1.16.2.linux-amd64.tar.gz -o go.tar.gz && tar -C /usr/local -xzf go.tar.gz
ENV PKG_CONFIG_PATH=/opt/py/lib/pkgconfig
COPY hello /hello
RUN cd /hello && /usr/local/go/bin/go get . && /usr/local/go/bin/go build -o /opt/hello/bin/hello .
# container to distribute the go program
FROM docker.io/library/debian:9-slim
COPY --from=builder-golang /opt/py /opt/py
COPY --from=builder-golang /opt/hello /opt/hello
RUN echo /opt/py/lib > /etc/ld.so.conf.d/py.conf && ldconfig
CMD /opt/hello/bin/hello
To try:
$ git clone https://github.com/christian-korneck/Dockerfiles
$ cd Dockerfiles/debian-go-python3-demo/
$ docker build -t hello-debian9 -f ./Dockerfile.debian9 .
$ docker run --rm hello-debian9
hello from python in go
from go-python3.
@christian-korneck thank you very much, --enable-shared
helps a lot.
from go-python3.
go-python3
works fine for me on Debian 10. In my setup the env var PKG_CONFIG_PATH
points to a dir with a file exactly named python3.pc
(I needed to create a symlink with that name).
Here's a Debian 10 Dockerfile example that works for me (even if you don't use Docker you should find the relevant parts in there):
FROM docker.io/library/debian:10
RUN apt-get -y update
RUN apt-get -y install golang python3.7 python3.7-dev pkg-config libpython3.7 libpython3.7-dev git
RUN ln -s /usr/lib/x86_64-linux-gnu/pkgconfig/python-3.7.pc /usr/lib/x86_64-linux-gnu/pkgconfig/python3.pc
ENV PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig
COPY hello /hello
RUN cd /hello && go get . && go build -o /usr/local/bin/hello .
CMD hello
(hello
is a go program that prints a python string, all files are here).
quickstart:
$ git clone https://github.com/christian-korneck/Dockerfiles
$ cd Dockerfiles/debian-go-python3-demo/
$ docker build -t hello-demo .
$ docker run --rm hello-demo
hello from python in go
Hope this helps.
from go-python3.
Hi, @christian-korneck , thank you very much for your patient.
I use debian 9 dokcer base image, python3.4 already installed on it. I can not changed to debian 10 for some reasons. And I noticed that apt-get install python3.7
and python3.7-dev
not works( Couldn't find any package by regex 'python3.7-dev'
), I've tried some ways to make it works such as add ppa
, all failed.
finally, I installed python3.7 by wget python3.7 source code and compile it. but go-python3
compile still failed(have set the PKG_CONFIG_PATH env). I think it is because python3.7-dev is not installed in my docker image, but I can not figure out how to install it even after google it for several days.
I am very appreciate for your help
from go-python3.
hi @christian-korneck ,i had the same problem ,and i used pyenv to install the python3.7 with options configure --enable-shared.but it still doesnt work. i can't use docker. thanks for your help~
os:ubuntu 20.04
from go-python3.
@floki2020 this works for me on Ubuntu 20.04:
apt-get -y update && apt-get -y install curl build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev git curl pkg-config gcc
# build/install Python 3.7 to /opt/py (note: this needs to be exactly Py 3.7)
curl https://www.python.org/ftp/python/3.7.10/Python-3.7.10.tgz -o python.tgz && tar xf python.tgz
cd Python-3.7.10
./configure --enable-shared --prefix=/opt/py
make -j `nproc`
make install
echo /opt/py/lib > /etc/ld.so.conf.d/py.conf && ldconfig
# build/install latest stable Go
curl -L https://golang.org/dl/go1.16.6.linux-amd64.tar.gz -o go.tar.gz && tar -C /usr/local -xzf go.tar.gz
export PKG_CONFIG_PATH=/opt/py/lib/pkgconfig
# fetch demo project
cd $HOME
git clone https://github.com/christian-korneck/Dockerfiles.git
cd Dockerfiles/debian-go-python3-demo/hello
# build demo project
/usr/local/go/bin/go get . && /usr/local/go/bin/go build -o /opt/hello/bin/hello .
# run demo project
/opt/hello/bin/hello
and you should see output
hello from python in go
(sorry if it's a bit clunky, copied from an ssh client on my phone. But hopefully it should get you an idea how to get it to work?)
from go-python3.
Related Issues (20)
- python3.PyImport_ImportModule(name) will emit a fatal error when called the second time. HOT 5
- How do I create an Object HOT 2
- fatal error: Python.h: No such file or directory #include "Python.h" HOT 4
- get PyObject's refcount
- Error: C.PyEval_ReInitThreads
- could not determine kind of name for C.PyImport_GetModule HOT 2
- How to stop or terminate python execution? HOT 1
- Hi, How to capture function return when use python3.PyRun_AnyFile HOT 2
- could not determine kind of name for C.PyDict_ClearFreeList HOT 16
- PyEval_ReInitThreads removed since python 3.8 HOT 2
- Undefined reference to 'Py_True' HOT 4
- No module named xxx problem ,when the xxx is a so file .. HOT 2
- How could I Pass parameters from the Go side to the python file? HOT 2
- run on windows HOT 3
- SyntaxError: invalid syntax (uuid.py, line 138) HOT 1
- PyList_Insert and PyList_Append functions has an extra b HOT 2
- Goroutine calls Python but deadlocks HOT 2
- how can i use tensorflow with library HOT 2
- How do I create a Python bytes object 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-python3.