@@ -10,7 +10,6 @@ Rust bindings for the NumPy C-API.
1010- [ Latest release] ( https://docs.rs/numpy )
1111- [ Current main] ( https://pyo3.github.io/rust-numpy )
1212
13-
1413## Requirements
1514- Rust >= 1.41.1
1615 - Basically, our MSRV follows the one of [ PyO3] ( https://github.com/PyO3/pyo3 )
@@ -23,92 +22,13 @@ Rust bindings for the NumPy C-API.
2322- [ numpy] ( https://numpy.org/ ) installed in your Python environments (e.g., via ` pip install numpy ` )
2423 - We recommend ` numpy >= 1.16.0 ` , though older versions may work
2524
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-
7325## Example
7426
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-
10827### Write a Python module in Rust
10928
11029Please see the [ simple-extension] ( https://github.com/PyO3/rust-numpy/tree/main/examples/simple-extension )
11130directory for the complete example.
31+
11232Also, we have an example project with [ ndarray-linalg] ( https://github.com/PyO3/rust-numpy/tree/main/examples/linalg ) .
11333
11434``` toml
@@ -117,11 +37,8 @@ name = "rust_ext"
11737crate-type = [" cdylib" ]
11838
11939[dependencies ]
40+ pyo3 = { version = " 0.15" , features = [" extension-module" ] }
12041numpy = " 0.15"
121-
122- [dependencies .pyo3 ]
123- version = " 0.15"
124- features = [" extension-module" ]
12542```
12643
12744``` rust
@@ -166,7 +83,66 @@ fn rust_ext(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
16683}
16784```
16885
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+
169144## Contributing
145+
170146We welcome [ issues] ( https://github.com/PyO3/rust-numpy/issues )
171147and [ pull requests] ( https://github.com/PyO3/rust-numpy/pulls ) .
172148
0 commit comments