To get the most out of this book
You will need a version of Python installed on your computer. All code examples have been tested using Python 3.11 on macOS. However, they should work with future version releases, too. Please note that Polars requires Python version 3.8 or higher.
Software/hardware covered in the book |
OS requirements |
Python >= 3.8 |
Windows, macOS, or Linux |
In any Python project, it is recommended that you use a virtual environment to isolate the dependencies of your different projects. This allows you to install libraries into an environment without affecting the global Python installation or any other environments.
The use of a virtual environment is not required, however, you can easily create one with venv
, which comes as part of the Python standard library. The following code creates a virtual environment:
$ python -m venv .venv
If you’re on macOS or Linux, the following code activates the virtual environment:
$ source .venv/bin/activate
You can use the following code for Windows:
$ .\.venv\Scripts\activate
There are several methods to install Polars and other Python libraries, however, in this book, we’ll be using pip, a popular package manager for Python.
You can either install the necessary libraries all at once or separately as you go. If you want to take the former approach, run the following code to install all the dependencies included in the requirements.txt
file:
$ pip install -r requirements.txt
If you want to take the latter approach, you’ll first need to install Jupyter Notebook since all the code examples are written in Jupyter notebooks, and the book assumes you will be running the code using Jupyter Notebook.
Install JupyterLab with pip:
$ pip install notebook
Launch the notebook as follows:
$ jupyter notebook
Alternatively, you can install JupyterLab with pip with the following command:
$ pip install jupyterlab
Once installed, you can launch JupyterLab with the following command:
$ jupyter lab
You can also refer to the instructions on the Jupyter website: https://jupyter.org/install.
Note that if you’re using an Integrated Development Environment (IDE) such as Visual Studio Code (VSCode), you may not need to launch the notebook. Instead, you can open it directly in VSCode once you have installed the notebook/JupyterLab via pip and necessary VSCode extensions as required.
Several other libraries will be used throughout the book, including the Polars library. In each chapter and recipe, you’ll be instructed to install these when necessary; however, it may be good to install the required libraries beforehand.
Use the following command to install Polars:
$ pip install polars
You can specify optional dependencies in brackets, as in the following command:
$ pip install polars[pyarrow, pandas]
To install all the optional dependencies of Polars, specify the term all
in the brackets:
$ pip install polars[all]
Also, while this most likely won’t be an issue, it is worth noting that your Polars should be version >= 1.0.0. That’s the version all the code in this book was tested on.
Now that you have set up your Python environment and have installed the dependencies, you’re ready to dive into the book contents.
If you are using the digital version of this book, we advise you to type the code yourself or access the code via the GitHub repository (link available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.
As Polars is growing rapidly day by day, it’s possible that some of the code snippets or methods and functions used in the book will be outdated by the time you’re reading it. We’ll do our best to note the new features and deprecations in the chapter notebooks, however, please feel free to contact us by sending an email or creating an issue in the GitHub repo.
Download the example code files
You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Polars-Cookbook/. If there’s an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!