rstudio / reticulate

R Interface to Python
https://rstudio.github.io/reticulate
Apache License 2.0
1.65k stars 326 forks source link

Python variables sourced via `reticulate::source_python()` are not contained within `py` list #1630

Open jooyoungseo opened 2 days ago

jooyoungseo commented 2 days ago

Current Behavior

Python variables sourced via reticulate::source_python() are not contained within py list.

Expected Behavior

Python variables called from Python repl needs to be contained within py list.

Reprex

  1. Create x.py with the following content:
x = 1
  1. Source x.py script like below:
# Define x in R
x <- "hello"
x
#> [1] "hello"

# Read python file
reticulate::source_python("C:/test/x.py")

# Print x again
## Python x overrides
x
#> [1] 1

Created on 2024-07-02 with reprex v2.1.0

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.4.1 (2024-06-14 ucrt) #> os Windows 11 x64 (build 22631) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United States.utf8 #> ctype English_United States.utf8 #> tz America/Chicago #> date 2024-07-02 #> pandoc 3.2.1 @ C:/PROGRA~3/CHOCOL~1/bin/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.3 2024-06-21 [1] CRAN (R 4.4.1) #> digest 0.6.36 2024-06-23 [1] CRAN (R 4.4.1) #> evaluate 0.24.0 2024-06-10 [1] CRAN (R 4.4.0) #> fastmap 1.2.0 2024-05-15 [1] CRAN (R 4.4.0) #> fs 1.6.4 2024-04-25 [1] CRAN (R 4.4.0) #> glue 1.7.0 2024-01-09 [1] CRAN (R 4.4.0) #> here 1.0.1 2020-12-13 [1] CRAN (R 4.4.0) #> htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.4.0) #> jsonlite 1.8.8 2023-12-04 [1] CRAN (R 4.4.0) #> knitr 1.47 2024-05-29 [1] CRAN (R 4.4.0) #> lattice 0.22-6 2024-03-20 [1] CRAN (R 4.4.1) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.4.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.4.0) #> Matrix 1.7-0 2024-04-26 [1] CRAN (R 4.4.1) #> png 0.1-8 2022-11-29 [1] CRAN (R 4.4.0) #> purrr 1.0.2 2023-08-10 [1] CRAN (R 4.4.0) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.4.0) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.4.0) #> R.oo 1.26.0 2024-01-24 [1] CRAN (R 4.4.0) #> R.utils 2.12.3 2023-11-18 [1] CRAN (R 4.4.0) #> Rcpp 1.0.12 2024-01-09 [1] CRAN (R 4.4.0) #> reprex 2.1.0 2024-01-11 [1] CRAN (R 4.4.0) #> reticulate 1.38.0 2024-06-19 [1] CRAN (R 4.4.1) #> rlang 1.1.4 2024-06-04 [1] CRAN (R 4.4.0) #> rmarkdown 2.27 2024-05-17 [1] CRAN (R 4.4.0) #> rprojroot 2.0.4 2023-11-05 [1] CRAN (R 4.4.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.4.0) #> styler 1.10.3 2024-04-07 [1] CRAN (R 4.4.0) #> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.4.0) #> withr 3.0.0 2024-01-16 [1] CRAN (R 4.4.0) #> xfun 0.45 2024-06-16 [1] CRAN (R 4.4.1) #> yaml 2.3.8 2023-12-11 [1] CRAN (R 4.4.0) #> #> [1] C:/Program Files/R/R-4.4.1/library #> #> ─ Python configuration ─────────────────────────────────────────────────────── #> python: C:/Users/jseo1005/OneDrive - University of Illinois - Urbana/Documents/.virtualenvs/r-reticulate/Scripts/python.exe #> libpython: C:/Users/jseo1005/AppData/Local/miniconda3/python311.dll #> pythonhome: C:/Users/jseo1005/OneDrive - University of Illinois - Urbana/Documents/.virtualenvs/r-reticulate #> version: 3.11.5 | packaged by Anaconda, Inc. | (main, Sep 11 2023, 13:26:23) [MSC v.1916 64 bit (AMD64)] #> Architecture: 64bit #> numpy: C:/Users/jseo1005/OneDrive - University of Illinois - Urbana/Documents/.virtualenvs/r-reticulate/Lib/site-packages/numpy #> numpy_version: 1.26.4 #> #> ────────────────────────────────────────────────────────────────────────────── ```
jooyoungseo commented 2 days ago

I have tested the dev version of reticulate in vanilla R session. Interestingly, even reticulate::repl_python() has the same issue on Windows 11:

Recording 2024-07-02 at 10 01 59

t-kalinowski commented 2 days ago

Thanks for opening. source_python() is meant to be behave similar to source(): it modifies variables in the global environment. The behavior you're seeing is intentional.

If you want to run python code and not export symbols from Python __main__ to the R global environment, please use reticulate::py_run_file().

jooyoungseo commented 2 days ago

@t-kalinowski Understood. Thanks for your explanation. Could you please also explain why objects created from reticulate::repl_python() is not contained within py list? It used to be contained within the list. Please refer to my gif above.

t-kalinowski commented 1 day ago

py is not an R list, it's an active binding that resolves objects from the Python __main__ module. E.g, if you run in Python __main__ x = 1, one way to then access x from R is with py$x.

jooyoungseo commented 1 day ago

@t-kalinowski I understood. However, why can't I access py$x in my gif example above? I created x = "py" within reticulate::repl_python(), but py$x does not exist once I come back to R session.