@@ -24,13 +24,13 @@ Now you can interact with Python as follows:
2424
2525``` julia-repl
2626julia> re = pyimport("re")
27- Python module : <module 're' from '[...]/lib/re.py'>
27+ Python: <module 're' from '[...]/lib/re.py'>
2828
2929julia> 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']
3131
3232julia> sentence = Py(" ").join(words)
33- Python str : 'PythonCall jl is very useful'
33+ Python: 'PythonCall jl is very useful'
3434
3535julia> pyconvert(String, sentence)
3636"PythonCall jl is very useful"
@@ -52,13 +52,13 @@ comparison and arithmetic:
5252
5353``` julia-repl
5454julia> x = pylist([3, 4, 5])
55- Python list : [3, 4, 5]
55+ Python: [3, 4, 5]
5656
5757julia> x[2] == 5
58- Python bool : True
58+ Python: True
5959
6060julia> x[pyslice(0,2)] + pylist([1,2])
61- Python list : [3, 4, 1, 2]
61+ Python: [3, 4, 1, 2]
6262```
6363
6464We have just seen the functions [ ` pylist ` ] ( @ref ) (for constructing a Python list) and
@@ -79,13 +79,13 @@ Notable exceptions are:
7979To access the Python builtins directly, you can access the fields of [ ` pybuiltins ` ] ( @ref ) :
8080``` julia-repl
8181julia> pybuiltins.None
82- Python None
82+ Python: None
8383
8484julia> pybuiltins.True
85- Python bool : True
85+ Python: True
8686
8787julia> pybuiltins.ValueError("some error")
88- Python ValueError : ValueError('some error')
88+ Python: ValueError('some error')
8989```
9090
9191With 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
103103
104104``` julia-repl
105105julia> x = pylist([3.4, 5.6])
106- Python list : [3.4, 5.6]
106+ Python: [3.4, 5.6]
107107
108108julia> pyconvert(Vector, x)
1091092-element Vector{Float64}:
@@ -116,7 +116,7 @@ julia> pyconvert(Vector{Float32}, x)
116116 5.6
117117
118118julia> pyconvert(Any, x)
119- 2-element PyList{Py }:
119+ 2-element PyList{Any }:
120120 3.4
121121 5.6
122122```
@@ -151,7 +151,7 @@ a Julia vector:
151151
152152``` julia-repl
153153julia> x = pylist([3,4,5])
154- Python list : [3, 4, 5]
154+ Python: [3, 4, 5]
155155
156156julia> y = PyList{Union{Int,Nothing}}(x)
1571573-element PyList{Union{Nothing, Int64}}:
@@ -176,7 +176,7 @@ julia> append!(y, 1:2)
176176 2
177177
178178julia> x
179- Python list : [3, 4, 5, None, 1, 2]
179+ Python: [3, 4, 5, None, 1, 2]
180180```
181181
182182There 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`:
187187
188188``` julia-repl
189189julia> x = pyimport("array").array("i", [3, 4, 5])
190- Python array : array('i', [3, 4, 5])
190+ Python: array('i', [3, 4, 5])
191191
192192julia> y = PyArray(x)
1931933-element PyArray{Int32, 1, true, true, Int32}:
@@ -202,7 +202,7 @@ julia> y[1] = 0
2022020
203203
204204julia> x
205- Python array : array('i', [0, 4, 5])
205+ Python: array('i', [0, 4, 5])
206206```
207207
208208It 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:
212212
213213``` julia-repl
214214julia> x = pyimport("io").StringIO()
215- Python StringIO : <_io.StringIO object at 0x000000006579BC70>
215+ Python: <_io.StringIO object at 0x000000006579BC70>
216216
217217julia> y = PyIO(x)
218218PyIO(<py _io.StringIO object at 0x000000006579BC70>, false, true, false, 4096, UInt8[], 4096, UInt8[])
@@ -222,10 +222,10 @@ julia> println(y, "Hello, world!")
222222julia> flush(y)
223223
224224julia> x.seek(0)
225- Python int : 0
225+ Python: 0
226226
227227julia> x.read()
228- Python str : 'Hello, world!\n'
228+ Python: 'Hello, world!\n'
229229```
230230
231231## [ Configuration] (@id pythoncall-config)
@@ -317,23 +317,44 @@ example package which wraps the Python FAISS package.
317317
318318### Precompilation
319319
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
321321``` julia
322322module MyModule
323323 using PythonCall
324324 const foo = pyimport (" foo" )
325325 bar () = foo. bar () # will crash when called
326326end
327327```
328- you must do
328+ you can do the import when the module is loaded, saving the result in a ` Ref `
329329``` julia
330330module MyModule
331331 using PythonCall
332- const foo = PythonCall . pynew () # initially NULL
332+ const foo = Ref {Py} ()
333333 function __init__ ()
334- PythonCall . pycopy! ( foo, pyimport (" foo" ) )
334+ foo[] = pyimport (" foo" )
335335 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)()
337358end
338359```
339360
0 commit comments