@@ -24,13 +24,13 @@ Now you can interact with Python as follows:
24
24
25
25
``` julia-repl
26
26
julia> re = pyimport("re")
27
- Python module : <module 're' from '[...]/lib/re.py'>
27
+ Python: <module 're' from '[...]/lib/re.py'>
28
28
29
29
julia> words = re.findall("[a-zA-Z]+", "PythonCall.jl is very useful!")
30
- Python list : ['PythonCall', 'jl', 'is', 'very', 'useful']
30
+ Python: ['PythonCall', 'jl', 'is', 'very', 'useful']
31
31
32
32
julia> sentence = Py(" ").join(words)
33
- Python str : 'PythonCall jl is very useful'
33
+ Python: 'PythonCall jl is very useful'
34
34
35
35
julia> pyconvert(String, sentence)
36
36
"PythonCall jl is very useful"
@@ -52,13 +52,13 @@ comparison and arithmetic:
52
52
53
53
``` julia-repl
54
54
julia> x = pylist([3, 4, 5])
55
- Python list : [3, 4, 5]
55
+ Python: [3, 4, 5]
56
56
57
57
julia> x[2] == 5
58
- Python bool : True
58
+ Python: True
59
59
60
60
julia> x[pyslice(0,2)] + pylist([1,2])
61
- Python list : [3, 4, 1, 2]
61
+ Python: [3, 4, 1, 2]
62
62
```
63
63
64
64
We have just seen the functions [ ` pylist ` ] ( @ref ) (for constructing a Python list) and
@@ -79,13 +79,13 @@ Notable exceptions are:
79
79
To access the Python builtins directly, you can access the fields of [ ` pybuiltins ` ] ( @ref ) :
80
80
``` julia-repl
81
81
julia> pybuiltins.None
82
- Python None
82
+ Python: None
83
83
84
84
julia> pybuiltins.True
85
- Python bool : True
85
+ Python: True
86
86
87
87
julia> pybuiltins.ValueError("some error")
88
- Python ValueError : ValueError('some error')
88
+ Python: ValueError('some error')
89
89
```
90
90
91
91
With the functions introduced so far, you have access to the vast majority of Python's
@@ -103,7 +103,7 @@ converted to another Julia type. Instead, you can explicitly convert using
103
103
104
104
``` julia-repl
105
105
julia> x = pylist([3.4, 5.6])
106
- Python list : [3.4, 5.6]
106
+ Python: [3.4, 5.6]
107
107
108
108
julia> pyconvert(Vector, x)
109
109
2-element Vector{Float64}:
@@ -116,7 +116,7 @@ julia> pyconvert(Vector{Float32}, x)
116
116
5.6
117
117
118
118
julia> pyconvert(Any, x)
119
- 2-element PyList{Py }:
119
+ 2-element PyList{Any }:
120
120
3.4
121
121
5.6
122
122
```
@@ -151,7 +151,7 @@ a Julia vector:
151
151
152
152
``` julia-repl
153
153
julia> x = pylist([3,4,5])
154
- Python list : [3, 4, 5]
154
+ Python: [3, 4, 5]
155
155
156
156
julia> y = PyList{Union{Int,Nothing}}(x)
157
157
3-element PyList{Union{Nothing, Int64}}:
@@ -176,7 +176,7 @@ julia> append!(y, 1:2)
176
176
2
177
177
178
178
julia> x
179
- Python list : [3, 4, 5, None, 1, 2]
179
+ Python: [3, 4, 5, None, 1, 2]
180
180
```
181
181
182
182
There are wrappers for other container types, such as [ ` PyDict ` ] ( @ref ) and [ ` PySet ` ] ( @ref ) .
@@ -187,7 +187,7 @@ like `bytes`, `bytearray`, `array.array` and `numpy.ndarray`:
187
187
188
188
``` julia-repl
189
189
julia> x = pyimport("array").array("i", [3, 4, 5])
190
- Python array : array('i', [3, 4, 5])
190
+ Python: array('i', [3, 4, 5])
191
191
192
192
julia> y = PyArray(x)
193
193
3-element PyArray{Int32, 1, true, true, Int32}:
@@ -202,7 +202,7 @@ julia> y[1] = 0
202
202
0
203
203
204
204
julia> x
205
- Python array : array('i', [0, 4, 5])
205
+ Python: array('i', [0, 4, 5])
206
206
```
207
207
208
208
It directly wraps the underlying data buffer, so array operations such as indexing are about
@@ -212,7 +212,7 @@ The [`PyIO`](@ref) wrapper type views a Python file object as a Julia IO object:
212
212
213
213
``` julia-repl
214
214
julia> x = pyimport("io").StringIO()
215
- Python StringIO : <_io.StringIO object at 0x000000006579BC70>
215
+ Python: <_io.StringIO object at 0x000000006579BC70>
216
216
217
217
julia> y = PyIO(x)
218
218
PyIO(<py _io.StringIO object at 0x000000006579BC70>, false, true, false, 4096, UInt8[], 4096, UInt8[])
@@ -222,10 +222,10 @@ julia> println(y, "Hello, world!")
222
222
julia> flush(y)
223
223
224
224
julia> x.seek(0)
225
- Python int : 0
225
+ Python: 0
226
226
227
227
julia> x.read()
228
- Python str : 'Hello, world!\n'
228
+ Python: 'Hello, world!\n'
229
229
```
230
230
231
231
## [ Configuration] (@id pythoncall-config)
@@ -317,23 +317,44 @@ example package which wraps the Python FAISS package.
317
317
318
318
### Precompilation
319
319
320
- You may not interact with Python during module precompilation. Therefore, instead of
320
+ You must not interact with Python during module precompilation. Therefore, instead of
321
321
``` julia
322
322
module MyModule
323
323
using PythonCall
324
324
const foo = pyimport (" foo" )
325
325
bar () = foo. bar () # will crash when called
326
326
end
327
327
```
328
- you must do
328
+ you can do the import when the module is loaded, saving the result in a ` Ref `
329
329
``` julia
330
330
module MyModule
331
331
using PythonCall
332
- const foo = PythonCall . pynew () # initially NULL
332
+ const foo = Ref {Py} ()
333
333
function __init__ ()
334
- PythonCall . pycopy! ( foo, pyimport (" foo" ) )
334
+ foo[] = pyimport (" foo" )
335
335
end
336
- bar () = foo. bar () # now ok
336
+ bar () = foo[]. bar ()
337
+ end
338
+ ```
339
+ or you can perform any imports dynamically
340
+ ``` julia
341
+ module MyModule
342
+ using PythonCall
343
+ bar () = pyimport (" foo" ). bar ()
344
+ end
345
+ ```
346
+ or if that is too slow, you can cache the import
347
+ ``` julia
348
+ module MyModule
349
+ using PythonCall
350
+ bar () = @pyconst (pyimport (" foo" )). bar ()
351
+ end
352
+ ```
353
+ or even cache the imported function
354
+ ``` julia
355
+ module MyModule
356
+ using PythonCall
357
+ bar () = @pyconst (pyimport (" foo" ). bar)()
337
358
end
338
359
```
339
360
0 commit comments