You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+20-7
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,12 @@ install the files. Julia 0.7 or later is required.
20
20
21
21
The latest development version of PyCall is available from
22
22
<https://github.com/JuliaPy/PyCall.jl>. If you want to switch to
23
-
this after installing the package, run `Pkg.checkout("PyCall"); Pkg.build("PyCall")`.
23
+
this after installing the package, run:
24
+
25
+
```julia
26
+
Pkg.add(PackageSpec(name="PyCall", rev="master"))
27
+
Pkg.build("PyCall")
28
+
```
24
29
25
30
By default on Mac and Windows systems, `Pkg.add("PyCall")`
26
31
or `Pkg.build("PyCall")` will use the
@@ -48,9 +53,9 @@ to the path of the `python` (or `python3` etc.) executable and then re-running `
48
53
In Julia:
49
54
50
55
ENV["PYTHON"] = "... path of the python executable ..."
51
-
# ENV["PYTHON"] = raw"C:\Python37-x64\python.exe" # example for Windows, "raw" to not have to escape: "C:\\Python37-x64\\python.exe"
56
+
# ENV["PYTHON"] = raw"C:\Python310-x64\python.exe" # example for Windows, "raw" to not have to escape: "C:\\Python310-x64\\python.exe"
52
57
53
-
# ENV["PYTHON"] = "/usr/bin/python3.7" # example for *nix
58
+
# ENV["PYTHON"] = "/usr/bin/python3.10" # example for *nix
54
59
Pkg.build("PyCall")
55
60
56
61
Note also that you will need to re-run `Pkg.build("PyCall")` if your
@@ -160,6 +165,8 @@ def sinpi(x):
160
165
py"sinpi"(1)
161
166
```
162
167
168
+
You can also execute a whole script `"foo.py"` via `@pyinclude("foo.py")` as if you had pasted it into a `py"""..."""` string.
169
+
163
170
When creating a Julia module, it is a useful pattern to define Python
164
171
functions or classes in Julia's `__init__` and then use it in Julia
165
172
function with `py"..."`.
@@ -192,7 +199,7 @@ cannot be accessed outside `MyModule`.
192
199
193
200
Here are solutions to some common problems:
194
201
195
-
* By default, PyCall [doesn't include the current directory in the Python search path](https://github.com/JuliaPy/PyCall.jl/issues/48). If you want to do that (in order to load a Python module from the current directory), just run `pushfirst!(PyVector(pyimport("sys")."path"), "")`.
202
+
* By default, PyCall [doesn't include the current directory in the Python search path](https://github.com/JuliaPy/PyCall.jl/issues/48). If you want to do that (in order to load a Python module from the current directory), just run `pushfirst!(pyimport("sys")."path", "")`.
196
203
197
204
## Python object interfaces
198
205
@@ -298,7 +305,7 @@ use `PyDict` as the return type of a `pycall` returning a dictionary,
298
305
or call `PyDict(o::PyObject)` on a dictionary object `o`. By
299
306
default, a `PyDict` is an `Any => Any` dictionary (or actually `PyAny
300
307
=> PyAny`) that performs runtime type inference, but if your Python
301
-
dictionary has known, fixed types you can insteady use `PyDict{K,V}` given
308
+
dictionary has known, fixed types you can instead use `PyDict{K,V}` given
302
309
the key and value types `K` and `V` respectively.
303
310
304
311
Currently, passing Julia dictionaries to Python makes a copy of the Julia
@@ -359,7 +366,8 @@ and also by providing more type information to the Julia compiler.
359
366
Python's [`eval`](https://docs.python.org/3/library/functions.html#eval) function, and returns the result
360
367
converted to `PyAny`. Alternatively, `py"..."o` returns the raw `PyObject`
361
368
(which can then be manually converted if desired). You can interpolate
362
-
Julia variables and other expressions into the Python code with `$`,
369
+
Julia variables and other expressions into the Python code (except for into
370
+
Python strings contained in Python code), with `$`,
363
371
which interpolates the *value* (converted to `PyObject`) of the given
364
372
expression---data is not passed as a string, so this is different from
365
373
ordinary Julia string interpolation. e.g. `py"sum($([1,2,3]))"` calls the
@@ -384,10 +392,15 @@ and also by providing more type information to the Julia compiler.
384
392
`__init__`. Side-effect in Python occurred at top-level Julia scope
385
393
cannot be used at run-time for precompiled modules.
386
394
395
+
You can also execute a Python script file `"foo.py"` by running `@pyinclude("foo.py")`, and it will be as if you had pasted the
396
+
script into a `py"..."` string. (`@pyinclude` does not support
397
+
interpolating Julia variables with `$var`, however — the script
398
+
must be pure Python.)
399
+
387
400
*`pybuiltin(s)`: Look up `s` (a string or symbol) among the global Python
388
401
builtins. If `s` is a string it returns a `PyObject`, while if `s` is a
389
402
symbol it returns the builtin converted to `PyAny`. (You can also use `py"s"`
390
-
to look up builtins or other Python globas.)
403
+
to look up builtins or other Python globals.)
391
404
392
405
Occasionally, you may need to pass a keyword argument to Python that
393
406
is a [reserved word](https://en.wikipedia.org/wiki/Reserved_word) in Julia.
0 commit comments