Thanks!
If you are using this format for your file, the knitr::read_chunk function should be what to used, as in the book's example. It will read the file, and split in code chunks based on the comment, then you can call each of them by there name in later chunks. As you call them with the python engine, them will have the correc language class for the highlighter to work.
Example:
---
title: Python chunks
output:
html_document:
highlight: tango
---
Read an external script:
```{r, include=FALSE, cache=FALSE}
knitr::read_chunk('test.py')
```
Now we can use the code, e.g.,
```{python, print-chunk}
```
```{python, add-chunk}
```
```{python}
add(2,2)
```
What is not good for what you are trying to do in this example ?
Did I miss something ?