Chapter 13 Python Environment

All the codes starting with $ should be run in the terminal or Anaconda Prompt.

For Windows users, we assume that you run the following steps in Anaconda Powershell Prompt.

For Mac users, we assume that you run the following steps in Mac terminal.

In this course, we use the Anaconda Python version. Necessary installation steps include:

Install the python with Anaconda

Please go to the official website of Anaconda for more information.

If you have installed Anaconda before, please make sure that you know where your default Anaconda Python interpreter path is. To check this:

  • Windows
    • Start Anaconda Prompt
    • Run $ conda activate environment-name (If you could like to check the python path of a particular conda environment)
    • Run $ where python
  • macOS
    • Open a terminal window.
    • Run $ source activate environment-name (If you could like to check the python path of a particular conda environment)
    • Run $ which python

Your default path should be similar to the following formats, where ALVIN would be your username and MYENV would be your conda environment name:

  • Windows default path: C:\Users\ALVIN\Anaconda3\python.exe or C:\Users\ALVIN\Anaconda3\envs\MYENV\python.exe
  • macOS default path: /Users/ALVIN/anaconda/bin/python

Create Conda Environment

Create a new conda environment with python 3.6+ to be used in R/Rstudio.

$ conda create --name XXX python=3.7

You need to specify the conda environment name XXX on your own.

Install Python Modules

Install all required python modules in this self-defined conda environment. There are usually two procedures:

  1. Activate the conda environment named XXX in your terminal;
## Mac
$ source activate XXX
## Window
$ conda activate XXX
  1. Install the module in the activated conda environment XXX in terminal.

In this course, we will need spacy and also the language models of English and Chinese. Please refer to spacy’s documentations of models for more details.

$ conda install spacy
$ python -m spacy download en_core_web_sm
$ python -m spacy download zh_core_web_sm

To deactivate the conda environment: $ conda deactivate XXX

This is for my reference.

If you run into error messages when you try to use a specific conda environment with use_condaenv(), it is probably due to the issue of reticulate failing to find the right conda path.

To fix this, there are two solutions:

  • For temporary setting:
options(reticulate.conda_binary="~/opt/anaconda3/bin/conda")
  • For permanent setting, include the following line in the .Renviron of your root directory ~/:
RETICULATE_CONDA=~/opt/anaconda3/bin/conda

Also, in RStudio, we can specify the default conda environment to be used in R. For each project, we can also specify a project-specific conda. This can be useful because we don’t have to use_condaenv() in the project then.