@@ -10,7 +10,6 @@ Rust bindings for the NumPy C-API.
10
10
- [ Latest release] ( https://docs.rs/numpy )
11
11
- [ Current main] ( https://pyo3.github.io/rust-numpy )
12
12
13
-
14
13
## Requirements
15
14
- Rust >= 1.41.1
16
15
- Basically, our MSRV follows the one of [ PyO3] ( https://github.com/PyO3/pyo3 )
@@ -23,92 +22,13 @@ Rust bindings for the NumPy C-API.
23
22
- [ numpy] ( https://numpy.org/ ) installed in your Python environments (e.g., via ` pip install numpy ` )
24
23
- We recommend ` numpy >= 1.16.0 ` , though older versions may work
25
24
26
- ** Note:**
27
- Starting from 0.3, rust-numpy migrated from rust-cpython to PyO3.
28
- If you want to use rust-cpython, use version 0.2.1 from crates.io.
29
-
30
-
31
- ## Python 2 support
32
- Version 0.5.0 is the last version that supports Python 2.
33
-
34
- If you want to compile this library with Python 2, please use 0.5.0 from crates.io.
35
-
36
- In addition, you have to add a feature flag in ` Cargo.toml ` like
37
- ``` toml
38
- [dependencies .numpy ]
39
- version = " 0.5.0"
40
- features = [" python2" ]
41
- ```
42
-
43
- You can also automatically specify the Python version in ` setup.py ` ,
44
- using [ setuptools-rust] ( https://github.com/PyO3/setuptools-rust ) .
45
-
46
-
47
- ## Dependency on ndarray
48
-
49
- This crate uses types from ` ndarray ` in its public API. ` ndarray ` is re-exported
50
- in the crate root so that you do not need to specify it as a direct dependency.
51
-
52
- Furthermore, this crate is compatible with multiple versions of ` ndarray ` and therefore depends
53
- on a range of semver-incompatible versions, currently ` >= 0.13, < 0.16 ` . Cargo does not
54
- automatically choose a single version of ` ndarray ` by itself if you depend directly or indirectly
55
- on anything but that exact range. It can therefore be necessary to manually unify these dependencies.
56
-
57
- For example, if you specify the following dependencies
58
-
59
- ``` toml
60
- numpy = " 0.15"
61
- ndarray = " 0.13"
62
- ```
63
-
64
- this will currently depend on both version ` 0.13.1 ` and ` 0.15.3 ` of ` ndarray ` by default
65
- even though ` 0.13.1 ` is within the range ` >= 0.13, < 0.16 ` . To fix this, you can run
66
-
67
- ``` sh
68
- cargo update ---package ndarray:0.15.3 --precise 0.13.1
69
- ```
70
-
71
- to achieve a single dependency on version ` 0.13.1 ` of ` ndarray ` .
72
-
73
25
## Example
74
26
75
-
76
- ### Execute a Python program from Rust and get results
77
-
78
- ``` toml
79
- [package ]
80
- name = " numpy-test"
81
-
82
- [dependencies ]
83
- pyo3 = " 0.15"
84
- numpy = " 0.15"
85
- ```
86
-
87
- ``` rust
88
- use numpy :: PyArray1 ;
89
- use pyo3 :: prelude :: {PyResult , Python };
90
- use pyo3 :: types :: IntoPyDict ;
91
-
92
- fn main () -> PyResult <()> {
93
- Python :: with_gil (| py | {
94
- let np = py . import (" numpy" )? ;
95
- let locals = [(" np" , np )]. into_py_dict (py );
96
- let pyarray : & PyArray1 <i32 > = py
97
- . eval (" np.absolute(np.array([-1, -2, -3], dtype='int32'))" , Some (locals ), None )?
98
- . extract ()? ;
99
- let readonly = pyarray . readonly ();
100
- let slice = readonly . as_slice ()? ;
101
- assert_eq! (slice , & [1 , 2 , 3 ]);
102
- Ok (())
103
- })
104
- }
105
-
106
- ```
107
-
108
27
### Write a Python module in Rust
109
28
110
29
Please see the [ simple-extension] ( https://github.com/PyO3/rust-numpy/tree/main/examples/simple-extension )
111
30
directory for the complete example.
31
+
112
32
Also, we have an example project with [ ndarray-linalg] ( https://github.com/PyO3/rust-numpy/tree/main/examples/linalg ) .
113
33
114
34
``` toml
@@ -117,11 +37,8 @@ name = "rust_ext"
117
37
crate-type = [" cdylib" ]
118
38
119
39
[dependencies ]
40
+ pyo3 = { version = " 0.15" , features = [" extension-module" ] }
120
41
numpy = " 0.15"
121
-
122
- [dependencies .pyo3 ]
123
- version = " 0.15"
124
- features = [" extension-module" ]
125
42
```
126
43
127
44
``` rust
@@ -166,7 +83,66 @@ fn rust_ext(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
166
83
}
167
84
```
168
85
86
+ ### Execute a Python program from Rust and get results
87
+
88
+ ``` toml
89
+ [package ]
90
+ name = " numpy-test"
91
+
92
+ [dependencies ]
93
+ pyo3 = { version = " 0.15" , features = [" auto-initialize" ] }
94
+ numpy = " 0.15"
95
+ ```
96
+
97
+ ``` rust
98
+ use numpy :: PyArray1 ;
99
+ use pyo3 :: prelude :: {PyResult , Python };
100
+ use pyo3 :: types :: IntoPyDict ;
101
+
102
+ fn main () -> PyResult <()> {
103
+ Python :: with_gil (| py | {
104
+ let np = py . import (" numpy" )? ;
105
+ let locals = [(" np" , np )]. into_py_dict (py );
106
+ let pyarray : & PyArray1 <i32 > = py
107
+ . eval (" np.absolute(np.array([-1, -2, -3], dtype='int32'))" , Some (locals ), None )?
108
+ . extract ()? ;
109
+ let readonly = pyarray . readonly ();
110
+ let slice = readonly . as_slice ()? ;
111
+ assert_eq! (slice , & [1 , 2 , 3 ]);
112
+ Ok (())
113
+ })
114
+ }
115
+
116
+ ```
117
+
118
+ ## Dependency on ndarray
119
+
120
+ This crate uses types from ` ndarray ` in its public API. ` ndarray ` is re-exported
121
+ in the crate root so that you do not need to specify it as a direct dependency.
122
+
123
+ Furthermore, this crate is compatible with multiple versions of ` ndarray ` and therefore depends
124
+ on a range of semver-incompatible versions, currently ` >= 0.13, < 0.16 ` . Cargo does not
125
+ automatically choose a single version of ` ndarray ` by itself if you depend directly or indirectly
126
+ on anything but that exact range. It can therefore be necessary to manually unify these dependencies.
127
+
128
+ For example, if you specify the following dependencies
129
+
130
+ ``` toml
131
+ numpy = " 0.15"
132
+ ndarray = " 0.13"
133
+ ```
134
+
135
+ this will currently depend on both version ` 0.13.1 ` and ` 0.15.3 ` of ` ndarray ` by default
136
+ even though ` 0.13.1 ` is within the range ` >= 0.13, < 0.16 ` . To fix this, you can run
137
+
138
+ ``` sh
139
+ cargo update ---package ndarray:0.15.3 --precise 0.13.1
140
+ ```
141
+
142
+ to achieve a single dependency on version ` 0.13.1 ` of ` ndarray ` .
143
+
169
144
## Contributing
145
+
170
146
We welcome [ issues] ( https://github.com/PyO3/rust-numpy/issues )
171
147
and [ pull requests] ( https://github.com/PyO3/rust-numpy/pulls ) .
172
148
0 commit comments