Setting Up Your Environment

We aim to lower the bar for starting empirical research in financial economics. We want that using Python is easy for you. However, given that Tidy Finance is a platform that supports multiple programming languages, we also consider the possibility that you are not familiar with Python at all. Maybe you transition from R to Python, i.e., follow 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 and a set of packages and dependencies. In order to isolate and manage the specific dependencies of the Tidy Finance with Python project, a 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 the book and maintain the 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 for Tidy Finance on your local system. After many trials and errors, we decided to use Python version 3.10.11 to set up the environment we used to write this book using both Windows and Mac. Thus, we recommend you also install that version. For the installation, we use the Anaconda Python distribution you downloaded in the step before. Additionally, you need our requirements.txt-file in a dedicated folder for the project. 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 requirements.txt file.

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 language, 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 plan to also give R a try, 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 that 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. Download or clone the Tidy Finance Environment repository
  3. Open environment.Rproj in the environment folder that you just downloaded (your R session will then automatically install the R package renv, if not run install.packages("renv"))
  4. Install the R package reticulate: install.packages("reticulate")
  5. Install Python via reticulate: reticulate::install_python(version="3.10.11", force = TRUE)
  6. 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 user name
  7. 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.

Colophon

Package Version
0 anyio 4.0.0
1 appdirs 1.4.4
2 appnope 0.1.3
3 argon2-cffi 23.1.0
4 argon2-cffi-bindings 21.2.0
5 arrow 1.3.0
6 astor 0.8.1
7 asttokens 2.4.0
8 async-lru 2.0.4
9 attrs 23.1.0
10 Babel 2.13.0
11 backcall 0.2.0
12 beautifulsoup4 4.12.2
13 bleach 6.1.0
14 certifi 2023.7.22
15 cffi 1.16.0
16 charset-normalizer 3.3.0
17 click 8.1.7
18 comm 0.1.4
19 contourpy 1.1.1
20 cycler 0.12.1
21 Cython 3.0.3
22 debugpy 1.8.0
23 decorator 5.1.1
24 defusedxml 0.7.1
25 et-xmlfile 1.1.0
26 exceptiongroup 1.1.3
27 executing 2.0.0
28 fastjsonschema 2.18.1
29 fonttools 4.43.1
30 formulaic 0.6.6
31 fqdn 1.5.1
32 frozendict 2.3.8
33 html5lib 1.1
34 idna 3.4
35 importlib-metadata 6.8.0
36 interface-meta 1.3.0
37 ipykernel 6.25.2
38 ipython 8.16.1
39 ipython-genutils 0.2.0
40 ipywidgets 8.1.1
41 isoduration 20.11.0
42 jedi 0.19.1
43 Jinja2 3.1.2
44 joblib 1.3.2
45 json5 0.9.14
46 jsonpointer 2.4
47 jsonschema 4.19.1
48 jsonschema-specifications 2023.7.1
49 jupyter 1.0.0
50 jupyter-cache 0.6.1
51 jupyter-console 6.6.3
52 jupyter-events 0.7.0
53 jupyter-lsp 2.2.0
54 jupyter_client 8.3.1
55 jupyter_core 5.4.0
56 jupyter_server 2.7.3
57 jupyter_server_terminals 0.4.4
58 jupyterlab 4.0.6
59 jupyterlab-pygments 0.2.2
60 jupyterlab-widgets 3.0.9
61 jupyterlab_server 2.25.0
62 kiwisolver 1.4.5
63 linearmodels 5.3
64 lxml 4.9.3
65 MarkupSafe 2.1.3
66 matplotlib 3.8.0
67 matplotlib-inline 0.1.6
68 mistune 3.0.2
69 mizani 0.9.3
70 multitasking 0.0.11
71 mypy-extensions 1.0.0
72 nbclient 0.7.4
73 nbconvert 7.9.2
74 nbformat 5.9.2
75 nest-asyncio 1.5.8
76 notebook 7.0.4
77 notebook_shim 0.2.3
78 numpy 1.26.0
79 openpyxl 3.1.2
80 overrides 7.4.0
81 packaging 23.2
82 pandas 2.1.1
83 pandas-datareader 0.10.0
84 pandocfilters 1.5.0
85 parso 0.8.3
86 patsy 0.5.3
87 peewee 3.16.3
88 pexpect 4.8.0
89 pickleshare 0.7.5
90 Pillow 10.0.1
91 platformdirs 3.11.0
92 plotnine 0.12.3
93 prometheus-client 0.17.1
94 prompt-toolkit 3.0.39
95 psutil 5.9.5
96 psycopg2-binary 2.9.9
97 ptyprocess 0.7.0
98 pure-eval 0.2.2
99 pycparser 2.21
100 Pygments 2.16.1
101 pyhdfe 0.2.0
102 pyparsing 3.1.1
103 python-dateutil 2.8.2
104 python-dotenv 1.0.0
105 python-json-logger 2.0.7
106 pytz 2023.3.post1
107 PyYAML 6.0.1
108 pyzmq 25.1.1
109 qtconsole 5.4.4
110 QtPy 2.4.0
111 referencing 0.30.2
112 regtabletotext 0.0.7
113 requests 2.31.0
114 rfc3339-validator 0.1.4
115 rfc3986-validator 0.1.1
116 rpds-py 0.10.4
117 scikit-learn 1.3.1
118 scipy 1.11.3
119 Send2Trash 1.8.2
120 setuptools-scm 7.1.0
121 six 1.16.0
122 sniffio 1.3.0
123 soupsieve 2.5
124 SQLAlchemy 2.0.21
125 stack-data 0.6.3
126 statsmodels 0.14.0
127 tabulate 0.9.0
128 terminado 0.17.1
129 threadpoolctl 3.2.0
130 tinycss2 1.2.1
131 tomli 2.0.1
132 tornado 6.3.3
133 traitlets 5.11.2
134 types-python-dateutil 2.8.19.14
135 typing_extensions 4.8.0
136 tzdata 2023.3
137 uri-template 1.3.0
138 urllib3 2.0.6
139 wcwidth 0.2.8
140 webcolors 1.13
141 webencodings 0.5.1
142 websocket-client 1.6.4
143 widgetsnbextension 4.0.9
144 wrapt 1.15.0
145 yfinance 0.2.31
146 zipp 3.17.0