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
+9-53Lines changed: 9 additions & 53 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -95,7 +95,7 @@ Here is a simple example to call Python's `math.sin` function and
95
95
compare it to the built-in Julia `sin`:
96
96
97
97
using PyCall
98
-
@pyimport math
98
+
math = pyimport("math")
99
99
math.sin(math.pi / 4) - sin(pi / 4) # returns 0.0
100
100
101
101
Type conversions are automatically performed for numeric, boolean,
@@ -104,12 +104,6 @@ arrays/lists, and dictionaries of these types. (Python functions can
104
104
be converted/passed to Julia functions and vice versa!) Other types
105
105
are supported via the generic PyObject type, below.
106
106
107
-
Python submodules must be imported by a separate `@pyimport` call, and
108
-
in this case you must supply an identifier to to use in Julia. For example
109
-
110
-
@pyimport numpy.random as nr
111
-
nr.rand(3,4)
112
-
113
107
Multidimensional arrays exploit the NumPy array interface for
114
108
conversions between Python and Julia. By default, they are passed
115
109
from Julia to Python without making a copy, but from Python to Julia a
@@ -120,7 +114,7 @@ Keyword arguments can also be passed. For example, matplotlib's
120
114
[pyplot](http://matplotlib.org/) uses keyword arguments to specify plot
121
115
options, and this functionality is accessed from Julia by:
122
116
123
-
@pyimport matplotlib.pyplot as plt
117
+
plt = pyimport("matplotlib.pyplot")
124
118
x = linspace(0,2*pi,1000); y = sin(3*x + 4*cos(2*x));
125
119
plt.plot(x, y, color="red", linewidth=2.0, linestyle="--")
126
120
plt.show()
@@ -133,7 +127,7 @@ Arbitrary Julia functions can be passed to Python routines taking
133
127
function arguments. For example, to find the root of cos(x) - x,
134
128
we could call the Newton solver in scipy.optimize via:
135
129
136
-
@pyimport scipy.optimize as so
130
+
so = pyimport("scipy.optimize")
137
131
so.newton(x -> cos(x) - x, 1)
138
132
139
133
A macro exists for mimicking Python's "with statement". For example:
@@ -147,23 +141,6 @@ conversion, use `f::PyObject`). Similarly, if the context manager returns a type
147
141
which is automatically converted to a Julia type, you will have override this
148
142
via `@pywith EXPR::PyObject ...`.
149
143
150
-
151
-
**Important:** The biggest difference from Python is that object attributes/members are
152
-
accessed with `o[:attribute]` rather than `o.attribute`, so that `o.method(...)` in
153
-
Python is replaced by `o[:method](...)` in Julia. Also, you use
154
-
`get(o, key)` rather than `o[key]`. (However, you can access integer
155
-
indices via `o[i]` as in Python, albeit with 1-based Julian indices rather
156
-
than 0-based Python indices.) (This will be changed to `o.method` once Julia
157
-
0.6 support is dropped, now that Julia supports `.` overloading.) See also the section on
158
-
`PyObject` below, as well as the `pywrap` function to create anonymous
159
-
modules that simulate `.` access (this is what `@pyimport` does). For
160
-
example, using [Biopython](http://biopython.org/wiki/Seq) we can do:
161
-
162
-
@pyimport Bio.Seq as s
163
-
@pyimport Bio.Alphabet as a
164
-
my_dna = s.Seq("AGTACACTGGT", a.generic_dna)
165
-
my_dna.find("ACT")
166
-
167
144
## Troubleshooting
168
145
169
146
Here are solutions to some common problems:
@@ -172,8 +149,8 @@ Here are solutions to some common problems:
172
149
173
150
## Python object interfaces
174
151
175
-
The `@pyimport` macro is built on top of several routines for
176
-
manipulating Python objects in Julia, via a type `PyObject` described
152
+
PyCall provides many routines for
153
+
manipulating Python objects in Julia via a type `PyObject` described
177
154
below. These can be used to have greater control over the types and
178
155
data passed between Julia and Python, as well as to access additional
179
156
Python functionality (especially in conjunction with the low-level interfaces
@@ -311,7 +288,7 @@ and the fact that the Julia JIT compiler can no longer infer the type).
311
288
312
289
### Calling Python
313
290
314
-
In most cases, the `@pyimport` macro automatically makes the
291
+
In most cases, PyCall automatically makes the
315
292
appropriate type conversions to Julia types based on runtime
316
293
inspection of the Python objects. However greater control over these
317
294
type conversions (e.g. to use a no-copy `PyArray` for a Python
@@ -331,13 +308,6 @@ and also by providing more type information to the Julia compiler.
331
308
`@pycall function(args...)::returntype` into
332
309
`pycall(function,returntype,args...)`.
333
310
334
-
*`pyimport(s)`: Import the Python module `s` (a string or symbol) and
335
-
return a pointer to it (a `PyObject`). Functions or other symbols
336
-
in the module may then be looked up by `s.name` where `name` is a
337
-
string (for the raw `PyObject`) or symbol (for automatic
338
-
type-conversion). Unlike the `@pyimport` macro, this does not
339
-
define a Julia module.
340
-
341
311
*`py"..."` evaluates `"..."` as a Python string, equivalent to
342
312
Python's [`eval`](https://docs.python.org/2/library/functions.html#eval) function, and returns the result
343
313
converted to `PyAny`. Alternatively, `py"..."o` returns the raw `PyObject`
@@ -363,18 +333,6 @@ and also by providing more type information to the Julia compiler.
363
333
symbol it returns the builtin converted to `PyAny`. (You can also use `py"s"`
364
334
to look up builtins or other Python globas.)
365
335
366
-
*`pywrap(o::PyObject)` returns a wrapper `w` that is an anonymous
367
-
module which provides (read) access to converted versions of `o`'s
368
-
members as `w.member`. (For example, `@pyimport module as name` is
369
-
equivalent to `name = pywrap(pyimport("module"))`.) If the Python
370
-
module contains identifiers that are reserved words in Julia
371
-
(e.g. `function`), they cannot be accessed as `w.member`; one must
372
-
instead use `w.pymember(:member)` (for the `PyAny` conversion) or
373
-
`w.pymember("member")` (for the raw `PyObject`). `pywrap` is rather
374
-
inefficient since it converts *every* member of `o` at once; you
375
-
are generally encouraged to simply access members via `o.member`
376
-
rather than using `pywrap`.
377
-
378
336
Occasionally, you may need to pass a keyword argument to Python that
379
337
is a [reserved word](https://en.wikipedia.org/wiki/Reserved_word) in Julia.
380
338
For example, calling `f(x, function=g)` will fail because `function` is
@@ -403,7 +361,7 @@ documentation `?pyfunction` and `?pyfunctionret` for more information.
403
361
`@pydef` creates a Python class whose methods are implemented in Julia.
404
362
For instance,
405
363
406
-
@pyimport numpy.polynomial as P
364
+
P = pyimport("numpy.polynomial")
407
365
@pydef mutable struct Doubler <: P.Polynomial
408
366
function __init__(self, x=10)
409
367
self.x = x
@@ -441,7 +399,7 @@ The method arguments and return values are automatically converted between Julia
441
399
Here's another example using [Tkinter](https://wiki.python.org/moin/TkInter):
442
400
443
401
using PyCall
444
-
@pyimport Tkinter as tk
402
+
tk = pyimport("Tkinter")
445
403
446
404
@pydef mutable struct SampleApp <: tk.Tk
447
405
__init__(self, args...; kwargs...) = begin
@@ -579,14 +537,12 @@ end
579
537
580
538
end
581
539
```
582
-
Then you can access the `scipy.optimize` functions as `scipy_opt[:newton]`
540
+
Then you can access the `scipy.optimize` functions as `scipy_opt.newton`
583
541
and so on.
584
542
585
543
Here, instead of `pyimport`, we have used the function `pyimport_conda`. The second argument is the name of the [Anaconda package](https://docs.continuum.io/anaconda/pkg-docs) that provides this module. This way, if importing `scipy.optimize` fails because the user hasn't installed `scipy`, it will either (a) automatically install `scipy` and retry the `pyimport` if PyCall is configured to use the [Conda](https://github.com/Luthaf/Conda.jl) Python install (or
586
544
any other Anaconda-based Python distro for which the user has installation privileges), or (b) throw an error explaining that `scipy` needs to be installed, and explain how to configure PyCall to use Conda so that it can be installed automatically. More generally, you can call `pyimport(module, package, channel)` to specify an optional Anaconda "channel" for installing non-standard Anaconda packages.
587
545
588
-
(Note that you cannot use `@pyimport` safely with precompilation, because that declares a global constant that internally has a pointer to the module. You can use `pywrap(pyimport(...))` in your `__init__` function to a assign a global variable using the `.` notation like `@pyimport`, however, albeit without the type stability of the global `const` as above.)
589
-
590
546
## Author
591
547
592
548
This package was written by [Steven G. Johnson](http://math.mit.edu/~stevenj/).
@@ -371,19 +387,13 @@ For example, `@pyimport module as name` is equivalent to `const name = pywrap(py
371
387
If the Python module contains identifiers that are reserved words in Julia (e.g. function), they cannot be accessed as `w.member`; one must instead use `w.pymember(:member)` (for the PyAny conversion) or w.pymember("member") (for the raw PyObject).
0 commit comments