jDataLab Jager

8 minute read

You are just getting started with Data Science, Machine Learning or Artificial Intelligence, and Python is one of the languages you have chosen, along with the R language. Right now you are working on setting up a Python environment in your Mac or PC. If all of the above mentioned are true, this post is a handy reference to setting up the most popular Python data science platform Anaconda in your local computer. Anaconda offers free individual edition, which currently the easiest way to learning from data with Python.

The benefits of using Anaconda come from its powerful components, which includes:

  • Both the classical Jupyter Notebook and a modern notebook interface JupyterLab support interactive development and reproducible work.
  • A bundled Python3 distribution, but you can still install other versions separately from Anaconda.
  • Conda is a package and environment management tool, which not only helps you create, load and switch between environments, but also makes it easy to find and install over 7500 packages.

Before continuing reading the post, check the following two notes:

  1. I assume you have previously installed a standalone Python in your local computer. If not, you can refer to another post and install Python first: Fully Remove Python and Install a Fresh Python in MacOS and Windows.
  1. We will install Visual Studio Code and Anaconda. If you have already installed either one or both and they are not working as expected, you may perform a complete removal of them as well as configurations and libraries. Then you may follow the guide and set up a brand new environment.

Therefore the guide first shows you, in Section 1 UNINSTALL, the removal of Visual Studio Code as well as Anaconda from MacOS and Windows, respectively. If you have neither one previously installed, you go directly to Section 2 SET UP THE PYTHON ENVIRONMENT FOR DATA SCIENCE.

Section 1. Uninstall

1. Remove Visual Studio Code and its Extensions

MacOS

The complete uninstall consists of four steps:

  1. Remove Visual Studio Code.app from Applications folder.
  2. Remove settings and configurations.
  3. Remove all the extensions.
  4. Clean the Trash.

To do so, open a Terminal (Reminder: Press Command+Space. Type Terminal. Open Terminal.app.). Run the following commands consecutively:

sudo rm -rf "/Applications/Visual Studio Code.app"
sudo rm -rf $HOME/Library/Application\ Support/Code
sudo rm -rf ~/.vscode
sudo rm -rf ~/.Trash/*

Windows

  1. Run the uninstall program unins000.exe in the directory of your VS Code. The location depends on the installer type, System or User. The default location of the System install is C:\Program Files\Microsoft VS Code.
  2. Remove settings and configurations: Delete the directory C:\Users\username\AppData\Roaming\Code
  3. Remove all the extensions: Delete the directory C:\Users\username\.vscode

2. Remove Anaconda and its footprints

You may follow the official guide of performing a deep clean of Anaconda. As indicated by the guide, a deep clean requires the operations in both Option B and A.

“If you also want to remove all traces of the configuration files and directories from Anaconda and its programs, you can download and use the Anaconda-Clean program first, then do a simple remove. See Option B.”

Also, the guide points out that anaconda-clean creates a backup folder .anaconda_backup.

“Anaconda-Clean creates a backup of all files and directories that might be removed in a folder named .anaconda_backup in your home directory. Also note that Anaconda-Clean leaves your data files in the AnacondaProjects directory untouched.”

The following lists the commands in Mac as well as the operations in Windows.

MacOS

  1. In the Terminal, run the two commands consecutively:

    • conda install anconda-clean
    • anaconda-clean –yes
  2. Remove the entire Anaconda directory, which has a name such as anaconda2, anaconda3, or ~/opt:

    • Find the name of Anaconda directory.
    • sudo rm -rf ~/opt (replace ~/opt with the directory name)
  3. Remove conda directories:

    • rm -rf ~/.condarc ~/.conda ~/.continuum
  4. Remove Anaconda path from .bash_profile:

    • Run the command in Terminal: open ~/.bash_profile
    • Remove the line similar to the following and having anaconda in the path, and save the change.
> export PATH="/Users/yourname/anaconda3/bin:$PATH"

Windows

  • In the Anaconda Navigator, launch the CMD.exe prompt terminal.
  • In the terminal, run the following commands consecutively:
conda install anconda-clean
anaconda-clean --yes
  • Locate the root directory of Anaconda. Run Uninstall-Anaconda3.exe (The Anaconda directory is similar to C:\Users\Jager\anaconda3)
  • In the Windows Explorer, manually remove the Anaconda directory.
  • (appwiz.cpl) In the Control Panel, choose Uninstall a program. Search python. Uninstall the Python record prefixed with Anaconda.
  • In the Windows Explorer, remove the shortcuts in the following folder: C:\Users\Jager\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Anaconda3 (64-bit)
  • Empty the Recycle Bin

SECTION 2. Set up the Python environment for Data Science

Assuming you have neither VS Code nor Anaconda in your computer. The following shows how to make a fresh setup with VS Code and Anaconda.

1. Install Visual Studio Code

To install Visual Studio Code, Microsoft’s free and cross-platform code editor, use the following procedures for MacOS and Windows, respectively.

MacOS

  1. Download the app from VS Code site

  2. Copy the app to the Applications folder:

    • cp -rf "$HOME/Downloads/Visual Studio Code.app" /Applications
  3. To open the app that is not allowed to open, manually overriding the settings in Security & Privacy preferences.

    “In the Finder on your Mac, locate the app you want to open. Control-click the app icon, then choose Open from the shortcut menu. Click Open. The app is saved as an exception to your security settings, and you can open it in the future by double-clicking it, just as you can any authorized app.”

  4. Test the Code with Python:

    • You must have a standalone Python install. If not, install one by following the post:

      Fully Remove Python and Install a Fresh Python in MacOS and Windows

    • In the Code:

      • Create a new file and save it with the extension py.
      • Follow the recommendation from the Code and install Microsoft Python extension.
      • Open the Command Palette. Search >Python:Select Interpreter. All the Python commands should appear in the list. You can select one for the current folder or file.
      • In the Terminal inside the Code, you can run pip to install Python packages into the currently selected Python.

Windows

  • In the VS Code site, there are two download options: user Installer for individual user and System Installer for all the users. Choose the one and install.

  • Run Python in the Code:

    • You must have a standalone Python install. If not, install one by following the post:

      Fully Remove Python and Install a Fresh Python in MacOS and Windows

    • In the Code:

      • Create a new file and save it with the extension py.
      • Follow the recommendation from the Code and install Microsoft Python extension.
      • Open the Command Palette. Search >Python:Select Interpreter. All the Python commands should appear in the list. You can select one for the current folder or file.
      • In the Terminal inside the Code, you can run pip to install Python packages into the currently selected Python.

2. Install Anaconda

MacOS

I recommend you follow The official instructions

Windows

I recommend you follow The official instructions

Take a glimpse of the following notes before launching the installer:

Do not install as Administrator unless admin privileges are required.

Install Anaconda to a directory path that does not contain spaces

“We recommend not adding Anaconda to the PATH environment variable, since this can interfere with other software. Instead, use Anaconda software by opening Anaconda Navigator or the Anaconda Prompt from the Start Menu.”

The default Anaconda folder is C:\Users\yourusername\anaconda3

Download Windows installer

3. How to run conda command

You will need to run conda commands in the last three sections. To run conda commands,

Windows: In the Anaconda Navigator, open CMD.exe Prompt

MacOS: Open the Terminal

SECTION 3 Working with Jupyter Notebook in Anaconda

Jupyter Notebook

  • Open Anaconda Navigator.app. ([the official guide] (https://docs.anaconda.com/anaconda/navigator/))
  • In the navigator, launch Jupyter notebook.
  • The default environment is base(root) with the bundled Python 3.8.5 within Anaconda as of the date when this post was written.
  • Create a new Jupyter notebook untitled.ipynb.
  • In the notebook, enter the script: import pandas as pd in the cell. Test the cell.

Package Management

You can run conda command in terminal to install a package. Refer to the official guide: Installing conda packages

I list some commands here for a quick reference.

conda install package-name
conda install package-name=x.y.z
conda install package-name=x.y.z -n environ-name

SECTION 4 Virtual Environments

Finally it is the time to learn how to management versions and packages in Anaconda. Anaconda includes Conda as a tool to manage applications, environments and packages.

Managing virtual environments with Conda

To view all the environments:

    conda info --envs
    conda env list

Besides the default environment base in Anaconda, we can create multiple virtual environments. Always check the official guide(https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html)

To create a new virtual environment:

    conda create -n py39 python=3.9 scikit-learn pandas numpy matplotlib

To switch to an environment, activate the environment by its name:

    conda activate py39

By default, the name in parentheses or brackets at the beginning of the prompt is the active environment. Anaconda by default is initiated with the base environment and it is shown in the terminal as (base) $.

To deactivate the current environment:

    conda deactivate

To use pip in an environment:

    conda install -n py39 pip
    conda activate py39
    pip install seaborn

To remove an environment, in your terminal window or an Anaconda Prompt, run:

    conda remove --name py39 --all
comments powered by Disqus