Import all Python libraries in one line of code | by Satyam Kumar | Towards Data Science

Python is a primary language used by data scientists for data science projects, because of the presence of thousands of open-source libraries that can ease and perform a data scientist task. Over 235,000 Python packages can be imported through PyPl.
Multiple libraries and frameworks need to be imported to perform tasks in a data science case study. Every time a data scientist or analyst starts a new jupyter notebook or any other IDE, they need to import all the libraries as per their requirements. Sometimes writing multiple lines of the same import statement over and over again can be frustrating. Here pyforest libraries come into the rescue, and it does the work for you.
Pyforest is an open-sourced Python library that enables data scientists to feel the bliss of automated imports. While working on a data science case study one needs to import multiple packages or libraries like pandas, matplotlib, seaborn, NumPy, SkLearn, etc. Importing all these libraries every time can be boring and disrupt the natural flow of your work. Also, you are free from searching for the exact import statement, like from sklearn.ensemble import RandomForestClassifier .
Using pyforest one can overcome these problems. Pyforest enables you to use all your favorite libraries without importing them. Pyforest does the work for you by automatically importing the libraries you want to use for your case study.
Once you import the pyforest library in one line, now you can use all the python libraries like one usually does. Any of the libraries that you use is not imported, Pyforest imports it for you automatically. The libraries are only imported once you call them or create an object of it. If a library is not used or called, it won’t be imported by pyforest.
Pyforest can be installed from Pypl using the following command:
pip install pyforest
After installing the library, you need to just import it in one line of code. Now you can use your favorite library like you usually do, without writing imports. In the below sample jupyter notebook, we have not imported pandas, seaborn and matplotlib library, still, we can use them by just importing the pyforest library.
import pyforest

data = pd.read_csv("titanic.csv")
data.head()

data.describe()

sns.countplot(data=data, x="Sex")
plt.show()

sns.heatmap(data.corr(), annot=True)

<matplotlib.axes._subplots.AxesSubplot at 0x7fe7c8e86278>
 

This package aims to add all the libraries that account for more than 99% of the imports, including popular libraries such as pandas as pd, NumPy as np, matplotlob.pyplot as plt, seaborn as sns, and many more. In addition to these libraries, it also some helper modules such as os, tqdm, re, and many more.
If you want to see the list of libraries, use dir(pyforest).
To add your own import statements to the pyforest libraries list, type in your explicit import statement to a file present in your home directory ~/.pyforest/user_imports.py .
Pyforest automatically the python libraries as and when required. Pyforest comes up with some functions to know the status of libraries:
active_imports(): Returns the list of libraries that have been imported and in use.
lazy_imports(): Return with the list of all libraries to import in the pyforest library.
In this article, we have discussed the pyforest library, an automated importing library. Using this library can reduce the stress to import tons of necessary libraries, instead, it does automatically imports the requirements. This library can be helpful for a data scientist who constantly works with new jupyter notebooks to explore their data science case study, now they can be free from importing the libraries, hence accelerating their workflow.
[1] Pyforest documentation (Apr 17, 2020): https://pypi.org/project/pyforest/
Loved the article? Become a Medium member to continue learning without limits. I’ll receive a small portion of your membership fee if you use the following link, with no extra cost to you.
satyam-kumar.medium.com
Thank You for Reading