Python troubleshooting
Charly Billaud, Junhong Yu
2024-12-12
Python_troubleshooting.Rmd
VertexWiseR uses Python functions from various toolboxes that can flexibly be called and executed in R using the reticulate package v1.37.0 (Ushey, Allaire, and Tang 2023). We explain in this article how to solve a number of issues that may arise from the interaction with Python.
1. About the message: “Would you like to create a default Python environment for the reticulate package? (Yes/no/cancel)”
As explained in the home page, this is a pop-up or prompt from the reticulate package. Choosing ‘Yes’ will let reticulate install Python in a virtual environment. This is entirely optional. Simply choose/click No/Cancel to ignore.
The reticulate documentation explains:
When installing Python packages it’s best practice to isolate >them within a Python environment (a named Python installation >that exists for a specific project or purpose). This provides a >measure of isolation, so that updating a Python package for one >project doesn’t impact other projects. The risk for package >incompatibilities is significantly higher with Python packages >than it is with R packages, because unlike CRAN, PyPI does not >enforce, or even check, if the current versions of packages >currently available are compatible.
Note that VertexWiseR will install a stable specific version of Python itself and of each package it needs to work properly. If users believe they will be using the Miniconda/Python installation for more than VertexWiseR and will need to update them, it may be safer to install a specific Miniconda/Python environment in a path of their choice (as allowed by VWRfirstrun()), or to use a reticulate isolated virtual environment. More details on why using virtual environments can be found in the reticulate documentation.
2. How to make VWRfirstrun() ignore the system’s Python installation
VertexWiseR will automatically select a previous Python installation
it finds via reticulate, but users may want to prevent this and run
VertexWiseR on a new Python installation of its own. This section
explains how to make VWRfirstrun()
ignore a previous
Python/Miniconda installation in order to install a new one (which will
not affect the system’s Python libraries).
Firstly, the Python installation path may already be preloaded in the
global environment, via files such as .Renviron
,
.Rprofile
, .bashrc
etc. If that is the case
they should be edited to remove the path of a previous Python
installation, because reticulate will likely read the paths contained in
these files.
Secondly, if the Python paths are not specified in the global environment, reticulate will then search for a Python installation in its default locations. This can be avoided with the method below.
To make VWRfirstrun()
ignore a previous Python
installation, users can set the RETICULATE_PYTHON
variable
as NA
(in a new R session with a clean workspace) before
running the function:
Sys.setenv(RETICULATE_PYTHON=NA)
VWRfirstrun()
With the above lines, reticulate will fail to load Python and respond
in the same way it would if Python was not previously installed at all.
Therefore, VWRfirstrun()
will promot users to install a new
Miniconda or Python environment. After that, VWRfirstrun()
will use that newly installed Python environment by default and users
should not need to do this again.
3. How to make VWRfirstrun() use a different Python installation
This section explains how users can choose a different Python other than the one automatically detected if they wish to.
a. For a new, fresh VertexWiseR set-up
Reticulate allows users to predefine the Python environment to be used by entering their Python’s directory path into the following functions:
reticulate::use_python(python)
reticulate::use_virtualenv(virtualenv)
reticulate::use_condaenv(condaenv)
reticulate::use_miniconda(condaenv)
These functions must be executed before VWRfirstrun()
is
run in the R session. The latter function should then automatically
assume that a version of Python is correctly installed and only ask for
other packages to be downloaded/installed if they are missing. To make
sure the correct Python installation is being used, users can run:
reticulate::py_config()
This will print the information and path of the Python environment reticulate is currently using. If that is still not the path specified in the functions listed above, users should try restarting R and/or clearing the workspace first.
b. If the VWRfirstrun() installations were already done before
If Python or Miniconda was already installed using
VWRfirstrun()
, users will not be able to modify the Python
path in the above manner. That is because VWRfirstrun()
saves the path to the user-selected default or custom installations in
order to conveniently access them again later. Every time
VWRfirstrun()
is run, it will read and use the saved
paths.
The paths are saved inside a .Renviron
file, in the
standard path generated via:
tools::R_user_dir(package='VertexWiseR')
Within the .Renviron
file, the environment variables
will be written in this manner:
RETICULATE_MINICONDA_PATH="C:/path_to_miniconda/"
RETICULATE_PYTHON_FALLBACK="C:/path_to_miniconda/"
RETICULATE_PYTHON="C:/path_to_miniconda/python.exe"
To swap with another Python environment, users may choose to edit
.Renviron
directly, by replacing the paths with the Python
installation they would like VertexWiseR to use instead. The
RETICULATE_MINICONDA_PATH
may also be safely removed if a
non-conda Python library is to be used. Alternatively, users may choose
to completely remove the .Renviron
and redefine the path as
described in section 3a.