Skip to content

Commit 46553be

Browse files
author
Randall C. O'Reilly
committed
added modules info and fixed example to include go.mod dummy creation etc, and added troubleshooting info.
1 parent 9b66a3f commit 46553be

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

README.md

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ New features:
1717

1818
## Installation
1919

20+
Gopy now assumes that you are working with modules-based builds, and requires a valid `go.mod` file, and works only with Go versions 1.15 and above.
21+
2022
Currently using [pybindgen](https://pybindgen.readthedocs.io/en/latest/tutorial/) to generate the low-level c-to-python bindings, but support for [cffi](https://cffi.readthedocs.io/en/latest/) should be relatively straightforward for those using PyPy instead of CPython (pybindgen should be significantly faster for CPython apparently). You also need `goimports` to ensure the correct imports are included.
2123

2224
```sh
@@ -37,7 +39,7 @@ python3 -m pip install --upgrade setuptools wheel
3739

3840
As of version 0.4.0, windows is now better supported, and is passing tests (on at least one developers machine). You may still need to set some environment variables depending on your python installation, but a vanilla standard install is working.
3941

40-
Install Python from the main Python distribution: https://www.python.org/downloads/windows/ -- *do not under any circumstances install from the Microsoft Store app!* while that is very convenient, it creates symbolic links to access the python executables, which is incompatible with go exec.Command to run it, despite too many hours of trying to get around that.
42+
Install Python from the main Python distribution: https://www.python.org/downloads/windows/ -- *do not install from the Microsoft Store app!* -- while that is very convenient, it creates symbolic links to access the python executables, which is incompatible with go exec.Command to run it, despite too many hours of trying to get around that.
4143

4244
The standard python install does not create a `python3.exe` which gopy looks for -- follow instructions here:
4345
https://stackoverflow.com/questions/39910730/python3-is-not-recognized-as-an-internal-or-external-command-operable-program/41492852
@@ -165,31 +167,14 @@ Options:
165167
166168
## Examples
167169
168-
### From the `python` shell
169-
170-
NOTE: following not yet working in new version:
171-
172-
`gopy` comes with a little `python` module allowing to wrap and compile `go`
173-
packages directly from the `python` interactive shell:
174-
175-
```python
176-
>>> import gopy
177-
>>> hi = gopy.load("github.com/go-python/gopy/_examples/hi")
178-
gopy> inferring package name...
179-
gopy> loading 'github.com/go-python/gopy/_examples/hi'...
180-
gopy> importing 'github.com/go-python/gopy/_examples/hi'
181-
182-
>>> print hi
183-
<module 'github.com/go-python/gopy/_examples/hi' from '/some/path/.../hi.so'>
184-
185-
>>> print hi.__doc__
186-
package hi exposes a few Go functions to be wrapped and used from Python.
187-
```
188-
189170
### From the command line
190171
172+
Note: you now need to make a go.mod file if you don't already have one in your environment, and get the package before building:
173+
191174
```sh
192-
$ gopy build -output=out -vm=`which python3` github.com/go-python/gopy/_examples/hi
175+
$ go mod init dummy.com/dum
176+
$ go get github.com/go-python/gopy/_examples/hi
177+
$ gopy build -output=out -vm=python3 github.com/go-python/gopy/_examples/hi
193178
$ ls out
194179
Makefile __init__.py __pycache__/ _hi.so* build.py go.py hi.c hi.go hi.py hi_go.h hi_go.so
195180
```
@@ -198,7 +183,7 @@ Makefile __init__.py __pycache__/ _hi.so* build.py go.py hi.c hi.go hi.p
198183
```sh
199184
$ cd out
200185
$ python3
201-
>>> import hi
186+
>>> from out import hi
202187
>>> dir(hi)
203188
['Add', 'Concat', 'Hello', 'Hi', 'NewPerson', 'Person', '__doc__', '__file__', '__name__', '__package__']
204189
@@ -214,6 +199,27 @@ go test -v -run=TestHi
214199
...
215200
```
216201
202+
### From the `python` shell (NOT YET WORKING)
203+
204+
NOTE: following not yet working in new version:
205+
206+
`gopy` comes with a little `python` module allowing to wrap and compile `go`
207+
packages directly from the `python` interactive shell:
208+
209+
```python
210+
>>> import gopy
211+
>>> hi = gopy.load("github.com/go-python/gopy/_examples/hi")
212+
gopy> inferring package name...
213+
gopy> loading 'github.com/go-python/gopy/_examples/hi'...
214+
gopy> importing 'github.com/go-python/gopy/_examples/hi'
215+
216+
>>> print hi
217+
<module 'github.com/go-python/gopy/_examples/hi' from '/some/path/.../hi.so'>
218+
219+
>>> print hi.__doc__
220+
package hi exposes a few Go functions to be wrapped and used from Python.
221+
```
222+
217223
## Binding generation using Docker (for cross-platform builds)
218224
219225
```
@@ -236,6 +242,14 @@ $ docker run -it --rm go-python/gopy
236242
To know what features are supported on what backends, please refer to the
237243
[Support matrix ](https://github.com/go-python/gopy/blob/master/SUPPORT_MATRIX.md).
238244
245+
## Troubleshooting
246+
247+
If you get an error like this after importing a generated module:
248+
```bash
249+
Fatal Python error: _PyInterpreterState_Get(): no current thread state
250+
```
251+
it means you are running a different version of python than the one that build the library you are importing -- make sure you've got the paths in your `-vm` arg aligned with what you are using to import.
252+
239253
## Contribute
240254
241255
`gopy` is part of the `go-python` organization and licensed under `BSD-3`.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/go-python/gopy
22

3-
go 1.13
3+
go 1.15
44

55
require (
66
github.com/gonuts/commander v0.1.0

0 commit comments

Comments
 (0)