Setting up a Python Development Workstation (MacOs)

101
en
setup
python
bash
macos
matplotlib
In this article, we start our journey into Finance and Python coding. Let’s set up our MacOs Workstation and install a few key tools to code efficiently.
Auteur

Vincent D.

Date de publication

20 février 2024

In Construction

Thanks for popping by; please note that this article is still in construction.

Introduction

Ready to enter the world of Python, Visual Studio, and financial analysis? Let’s embark on this journey together. By the end of this guide, you’ll have your finance lab all set for dissecting market trends, optimising portfolios, and much more. Buckle up, let’s set sail! This article requires some knowledge of the terminal, installing softwares

Step 1: Install Your General Tooling

Whilst not mandatory, before looking at Python, code editors and various libraries, it’s extremely useful to check that your MacOs is ready to provide you with the best general setup. Terminal windows, bash script might look a bit obscure at first glance, but take it step by step and look at the video linked below to get more clarity on this. This is a marathon, definitely not a sprint.

1.1 Install Homebrew

Homebrew, affectionately known as brew, is the Swiss Army knife for installing software on macOS. It simplifies the process of managing software on your Mac, making it a breeze to install, uninstall, and update your applications.

To install Homebrew, open Terminal and enter:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

1.2 Install iTerm2

iTerm2 is an improvement over macOS’s default terminal, offering features such as split panes, search, and customisation. Install it via brew with:

brew install --cask iterm2

1.3 Install Oh My Zsh:

Zsh, combined with Oh My Zsh, turns the dull default terminal into a powerful and enjoyable tool, complete with themes, plugins, and a vibrant community.

Install Oh My Zsh with:

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Step 2. Setting Up Visual Studio and Miniconda

2.1 Visual Studio

Visual Studio Code (VS Code) is a lightweight but powerful source code editor that supports debugging, embedded Git control, syntax highlighting, and more. Install VS Code via brew:

VS Code is a robust and versatile code editor, while Miniconda, a mini version of Anaconda, allows us to create isolated Python environments and manage packages.

Using Brew, installed above, is a breeze

brew install --cask visual-studio-code

Alternatively, you can download and install Visual Studio Code from the official website. Follow the prompts to complete the installation process.

2.2 Miniconda

Using Brew

brew install --cask miniconda

Alternatively, you can download and install Miniconda from the official website. During installation, ensure that you select the option to “Add Miniconda to my PATH environment variable”. This allows you to use conda commands directly in the VS Code terminal.

Step 3. Setting Up a Python Virtual Environment

3.1 Create the Conda environment

With our tools installed, let’s now create a virtual environment to house our finance lab. We’ll use conda to create a new environment named portfolio-geek using Python 3.10.

Open VS Code, then open a terminal and type the following command:

conda create -n portfolio-geek python=3.10

Activate the portfolio-geek environment by typing:

conda activate portfolio-geek

3.2 Install the key packages for your coding journey

Inside portfolio-geek, we install several vital Python libraries for our financial analysis journey:

  • pandas: A flexible and powerful data manipulation library, essential for handling and processing financial data.

  • matplotlib and seaborn: Two formidable libraries for data visualisation, because finance isn’t just about numbers—it’s about insightful charts too!

  • yfinance: An effective library for fetching historical stock data from Yahoo Finance.

  • pyportfolioopt: An advanced library designed to optimise portfolios, calculate risk and returns, and much more.

Install these libraries using the following command:

conda install pandas matplotlib seaborn yfinance pyportfolioopt

Step 4. Installing Key Visual Studio Extensions

This step is not mandatory but it will be of tremendous help on a day to day basis. VS Code shines with its extensions. Here are a few that will make your life easier:

  • Python: Gives you a host of Python-specific features like linting, debugging, code formatting, and more.

  • Prettier: A code formatter that supports many languages, including Python. Keeps your code clean and professional.

  • Code Spell Checker: Like a proof-reader, it catches common spelling mistakes in your code.

You can install these extensions by searching for them in the VS Code Extensions view (Ctrl + Shift + X) and clicking on “Install”.

Step 5 Launching a Jupyter Notebook Inside Visual Studio

With our portfolio-geek environment and VS Code set up, it’s time to dive into some coding. We’ll be using a Jupyter Notebook, a powerful tool allowing you to interweave code and text into a single document.

Create a new file with a .ipynb extension. Upon creation, VS Code will prompt you to select a Python interpreter—choose the one corresponding to our portfolio-geek environment.

Code
# Ensure you're in the 'portfolio-geek' environment

import matplotlib.pyplot as plt
import numpy as np

# Sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Plotting
plt.plot(x, y)
plt.title("Simple Sine Wave Plot")
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.show()