When developing in Python, it is generally a good practice not to rely on the Python version that ships with the operating system (OS). This is to ensure that the system version of Python remains relatively ‘clean’ for the OS processes. In addition, by installing a custom version(s) of Python, we open many possibilities. For one, it gives us control over which specific version(s) to use in our projects, and two, by using a virtual environment manager, we ensure that each project has access to its own tailored list of packages. One way of achieving this is by using pyenv.
How does pyenv works?
pyenv works is that every time we issue a Python command, it intercepts this call, using shim executables injected in our environment’s
PATH, and redirects the call to the correct Python installation based on our configurations.
How to install pyenv?
Personally, on macOS, I prefer to use HomeBrew.
This command will install both
pyenv-virtualenv which is a virtual environment manager for Python.
We also need to add
pyenv to our shell
~/.zshrc to enable shims and autocompletion:
Once added, restart your terminal or execute
exec zsh to reload the above configuration.
pyenv is installed, we can list all the available versions of Python (there are many!) by issuing the following command.
To install one of these versions (for example 3.9.4), we would normally use
However, to use the environment with
reticulate package in RStudio, we need to use
env PYTHON_CONFIGURE_OPTS="--enable-shared" with the above command, otherwise we may receive the following error when loading
reticulate in R:
This is to satisfy one of the requirements of
reticulate as specified here.
Note that for reticulate to bind to a version of Python it must be compiled with shared library support (i.e. with the –enable-shared flag).
To proceed with installing Python
How to prepare a Python environment for
First, create an environment named, for example,
PyRStudio that is based on the Python version we installed above (
pyenv activate PyRStudio to manually activate the environment, or if you prefer to activate this environment automatically every time we change into the project directory, do:
One last step is to install
numpy in the activated environment.
Launch a new Rstudio session, e.g.
open -a Rstudio and check whether
reticulate can recognise the activated environment.
numpy in a new Python code chunk:
How to revert this setup?
- To uninstall the virtual environment, use:
- To uninstall both the virtual environment and the custom Python version (3.9.4 in our example), use:
- To uninstall
Additionally, delete/comment the
pyenv configuration commands that we added to
~/.zshrc during the installation step above.