Learn everything about Analytics

Home » A Quick Guide to Setting up a Virtual Environment for Machine Learning and Deep Learning on macOS

A Quick Guide to Setting up a Virtual Environment for Machine Learning and Deep Learning on macOS

Introduction

Upgrading either Anaconda or Python on macOS is complicated. But using the process explained below will ease it out. For this, I’m assuming you have a clean system without any installation of Anaconda or Python (except Python 2.7 and Python 3.8.2 as these come pre-installed with macOS Big Sur). If you have already installed Anaconda or Python, uninstall them using any mac App Cleaner. Without much delay, let’s get started.

Setup Process

  • Install Homebrew from here or follow along. Homebrew is a package manager for macOS just like apt-get is for Debian (Linux) or pip
    is for python.

    • To install homebrew, open the terminal and copy-paste the following code:
    • We will update and upgrade brew just to make sure everything is in place. To do that, copy-paste the below code:
      • brew update && brew upgrade
    • Now, we will add homebrew’s directory to the PATH environment variable
      • echo ‘export PATH=”/usr/local/sbin:$PATH”‘ >> ~/.zshrc
    • Exit the terminal and open it again.
  • After installing brew, it’s time to install python using brew and it will be very easy to upgrade python using the brew in the future if a new release comes out which we will talk about later.
    • To install python, we will invoke the following command on the terminal:
      • brew install python
    • After installing python, once again we will exit the terminal and re-open so that the newly created python3’s symlinks are accessible by the terminal.
  • Now, type python3 on the terminal, it will show the latest version of python3 which in this case is python 3.9.1
Virtual Environment macos setup
  • As a matter of fact, starting with Python 3.4, pip is included by default with the Python binary installer. So, we don’t need to explicitly install it. But we will run an upgrade command to make sure pip and a few other packages are up-to-date.
    Type the below code in the terminal:

    • pip3 install –upgrade pip setuptools wheel
Virtual Environment pip 2 installation
  • Now, we will install the virtual environment, read more about the virtual environment here. In short, creating a virtual environment will allow us to avoid installing Python packages globally which could break system tools or other projects.
    • python3 -m pip install virtualenv
  • To create a virtual environment, select a folder or project location. In this case, we will create the virtual environment in the home directory.
    • python3 -m venv ~/pyenv
  • Before we can start installing or using packages in the virtual environment we’ll need to activate them. Activating a virtual environment will put the virtual environment-specific python and pip executables into our shell’s PATH.
    • source ~/pyenv/bin/activate
    • python3 -m pip install –upgrade pip setuptools wheel
pip upgrade
  • Now, we will install the required python and machine learning packages for a head start:
    • pip install requests jupyterlab jupyter notebook numpy pandas matplotlib seaborn scipy sklearn beautifulsoup4
  • If you need to install more packages, you can do so via pip3 install package-name. Just make sure that every time you do so, you have activated the virtual environment. That’s it!

 

Activate the virtual environment and run the jupyter notebook

  • For simplicity, I’m assuming you have closed the terminal or restarted your system. Open the terminal and type the following:
    • source ~/pyenv/bin/activate jupyter notebook
activate virtual environment
  • You have successfully run the jupyter notebook.

My name is Siddharth Shankar. I’m an analytical professional with 2+ years of experience in managing day to day operational and technical aspects of the project along with data compiling, analysis and reporting. I’m a detail-oriented organized individual with a background in Statistics coupled with Software Engineering and PC Technician A+ certifications. To know more visit: https://www.linkedin.com/in/Sshankar7/

 

 

You can also read this article on our Mobile APP Get it on Google Play