Setting Up Your Environment

We aim to lower the bar for starting empirical research in financial economics. We want to make using Python easy for you. However, given that Tidy Finance is a platform that supports multiple programming languages, we also consider the possibility that you are unfamiliar with Python. Maybe you are transitioning from R to Python, i.e., following the journey of Tidy Finance, which started in R. Hence, we provide you with a simple guide to get started with Python. If you have not used Python before, you will be able to use it after reading this chapter.

Python Environment

A Python environment is a self-contained directory or folder containing a specific version of the Python installation with a set of packages and dependencies. In order to isolate and manage the specific dependencies of the Tidy Finance with Python project, a so-called virtual environment is a reliable way to ensure that it will work consistently and reliably on different systems and over time.

There are many ways to install a Python version and environments on your system. We present two ways that we found most convenient to write this book and maintain our website: (i) Installation via Anaconda along with using Python in Spyder and (ii) installation via RStudio.

Installation via Anaconda

First, we need to install Python itself via Anaconda. You can download the latest version of Anaconda from the official Anaconda website. After downloading the respective version for your operating system, follow the installation instructions.

Second, we now describe how to set up a Python virtual environment specific to Tidy Finance on your local system. This book uses Python version 3.10.11 to set up the environment for both Windows and Mac. As we write this book, it is not the latest version of Python. The reason for this is that we wanted (i) a stable code base and (ii) the content of the book to be usable for all kinds of users, especially for those who might rely on corporate version controls and are not able to install new Python distributions.

For the installation, we use the Anaconda Python distribution you downloaded in the step before.1 Additionally, you need the packages listed in the provided requirements.txt-file in a dedicated folder for the project. You can find the detailed list of packages in the Colophon.

We recommend you start with the package installation right away. After you have prepared your system, you can open the Anaconda prompt and install your virtual environment with the following commands:

  1. conda create -p C:\Apps\Anaconda3\envs\tidy_finance_environment python==3.10.11 (Confirm with y)
  2. conda activate C:\Apps\Anaconda3\envs\tidy_finance_environment
  3. pip install -r "<Tidy-Finance-with-Python Folder>\requirements.txt"

All other packages found with the command pip list are installed automatically as dependencies with the required packages in the file requirements.txt. Note that we make reference to two distinct folders. The first one, C:\Apps\Anaconda3\envs\tidy_finance_environment refers to the location of your Python environment used for Tidy Finance. Apart from that, you should store your data, program codes, and scripts in another location: Your Tidy Finance working folder.

Now, you are basically ready to go. However, you will now need a Python integrated development environment (IDE) to make your coding experience pleasant.

Python IDE

If you are new to coding, you will not have an IDE for Python. We recommend using Spyder if you plan to code only in Python as it comes with Anaconda. If you don’t use Anaconda, you can download the software for your operating system from the official website. Then, follow the installation instructions. To add the previously created virtual environment to Spyder, Go to Tools → Preferences → Python Interpreter → “Use the following interpreter” and add C:\Apps\Anaconda3\envs\tidy_finance_environment\python.exe.

Another increasingly popular code editor for data analysis is Visual Studio Code (VS Code), as it supports a variety of programming languages, including Python and R. We refer to this tutorial if you want to get started with VS Code. There are many more ways to set up a Python IDE, so we refer to this page in the Python wiki for more inspiration.

If you also plan to try R, you should get a multipurpose tool: RStudio. You can get your RStudio version from Posit (i.e., the company that created RStudio, which was previously called RStudio itself). When you follow the instructions, you will see that Posit asks you to install R; you need to do so to make RStudio feasible for Python. Then, select the virtual environment in RStudio. Alternatively, you can also start with the installation guide starting from RStudio, which we present below.

Installation via RStudio

You can also install Python and set up your environment directly from RStudio. This approach has the big advantage that you can switch between R and Python code smoothly. We believe that being able to switch between different programming languages is a tremendously valuable skill, so we set up a repository containing all the files that you need to achieve this goal: Tidy Finance Environment. To set up this environment locally, follow these steps:

  1. Install R and RStudio.
  2. Clone the Tidy Finance Environment repository directly in RStudio by clicking on File/New Project/ and selecting Version Control. Then, click Git and provide the repository address https://github.com/tidy-finance/environment. RStudio will then automatically open the project in the new environment.
  3. Install the reticulate R package: install.packages("reticulate").
  4. Use reticulate to install Python: reticulate::install_python(version = "3.10.11", force = TRUE).
  5. Tell renv to use Python: renv::use_python("PATH").
    1. "PATH" on Mac: "~/.pyenv/versions/3.10.11/bin/python".
    2. "PATH" on Windows: "C:/Users/<User>/AppData/Local/r-reticulate/ r-reticulate/pyenv/pyenv-win/versions/3.10.11/python.exe" where <User> is your username.
  6. Tell renv to install all required packages: renv::restore().

Now you are ready to execute all code that you can find in this book or its sibling Tidy Finance with R.

Footnotes

  1. Note that you can also install a newer version of Python. We only require the environment set up in the previous step to use Python version 3.10.11. The neat aspect is Python’s capability to accommodate version control in this respect.↩︎