pyforest lazy-imports all popular Python Data Science libraries so that they are always there when you need them. If you don't use a library, it won't be imported. When you are done with your script, you can export the Python code for the import statements.

Demo in Jupyter Notebook


Demo in Python Shell


Using pyforest

pyforest lazy-imports all popular Python Data Science libraries with a single line of code:

from pyforest import *

And if you use Jupyter or IPython, you can even skip this line because pyforest adds itself to the autostart.

When you are done with your script, you can export all import statements via:


Which libraries are available?

  • We aim to add all popular Python Data Science libraries which should account for >99% of your daily imports. For example, pandas as pd, numpy as np, seaborn as sns, matplotlib.pyplot as plt, or OneHotEncoder from sklearn and many more. In addition, there are also helper modules like os, re, tqdm, or Path from pathlib.
  • You can see an overview of all available lazy imports if you type lazy_imports() in Python.
  • If you are missing an import, you can add it to the pyforest imports.

In order to gather all the most important names, we need your help. Please open a pull request and add the imports that we are still missing.


You need Python 3.6 or above because we love f-strings.

From the terminal, enter:

pip install pyforest

And you're ready to go.

Please note, that this will also add pyforest to your IPython default startup settings.

Frequently Asked Questions

  • "I need to always explicitly write down the libraries I used at the top of my scripts."

    • Of course, you can export the import statements for all used libraries with active_imports().
  • "Doesn't this slow down my Jupyter or Python startup process?"

    • No, because the libraries will only be imported when you actually use them. Until you use them, the variables like pd are only pyforest placeholders.
  • "Why can't I just use the typical IPython import?"

    • If you were to add all the libraries that pyforest includes, your startup time might take more than 30s.
  • "I don't have and don't need tensorflow. What will happen when I use pyforest?"

    • Tensorflow is included in pyforest but pyforest does not install any dependencies. You need to install your libraries separately from pyforest. Afterwards, you can access the libraries via pyforest if they are included in the pyforest imports.
  • "Will the pyforest variables interfere with my own local variables?"

    • Please make sure that you import pyforest at the beginning of your script. Then you will always be safe. You can use your variables like you would without pyforest. The worst thing that can happen is that you overwrite a pyforest placeholder and thus cannot use the placeholder any more (duh).
  • "What about auto-completion on lazily imported modules?"

    • It works :) As soon as you start the auto-completion, pyforest will import the module and return the available symbols to your auto-completer.
  • "How to (temporarily) deactivate the auto_import in IPython and Jupyter?"

    • Go to the directory ~/.ipython/profile_default/startup and adjust or delete the file. You will find further instructions in the file.
  • "How to (re)activate the pyforest auto_import?"

    • Execute the following Python command in Jupyter, IPython or Python: from pyforest.auto_import import setup; setup(). Please note that the auto_import only works for Jupyter and IPython.
  • "Why is pandas_profiling also imported in the demo?"

    • pyforest supports complementary, optional imports. For example, pandas_profiling patches the pd.DataFrame with the convenience function df.profile_report. Therefore, pyforest also imports pandas_profiling if you have it installed. If you don't have pandas_profiling installed, the optional import will be skipped.
  • "I don't want to copy complementary import statements to the top of my file."

    • Please note, that the complementary imports will always appear at the bottom of the import_statements list. So, you can just copy all statements above. Alternatively, you can deactivate complementary imports.
  • "How to deactivate complementary imports?"

    • You can uncomment the statements *.__on_import__() at the bottom of the pyforest imports file.
  • "Why is the project called pyforest?"

    • In which ecosystem do pandas live?


In order to gather all the most important names, we need your help. Please open a pull request and add the imports that we are still missing to the pyforest imports. You can also find the guidelines in the pyforest imports file

Using pyforest as Package Developer

pyforest helps you to minimize the (initial) import time of your package which improves the user experience. If you want your package imports to become lazy, rewrite your imports as follows:


import pandas as pd


from pyforest import LazyImport
pd = LazyImport("import pandas as pd")