Pyforest Is a lazy Python library.

All Python libraries (already installed locally) can be imported with one line of code.

GitHub address: github.com/8080labs/py…

/ 01 / Introduction

Python is popular because it has thousands of powerful open source libraries.

Currently, more than 235,000 Python libraries can be imported through PyPl, a huge number.

In common practice, it is common to import multiple libraries or frameworks to perform tasks.

And every time you create a new program file, you need to import related libraries according to your own needs.

If it’s the same type of task, like doing a small data visualization project, you might go all the way to a library.

If you write the same import statement over and over again, even if you copy and paste it, it will feel troublesome, and the Pyforest library will come into play.

Pyforest is an open source Python library that automatically imports Python libraries used in your code.

For data visualization, it is common to import multiple libraries, such as pandas, Numpy, matplotlib, and so on.

With Pyforest, you don’t need to import the same Python library in each program file, and you don’t need to use exact import statements.

This line of code, for example, can be omitted.

from sklearn.ensemble import RandomForestClassifier
Copy the code

After you import the Pyforest library with the import statement, you can use all Python libraries directly.

import pyforest

df = pd.read_csv('test.csv')print(df)
Copy the code

Any libraries you use do not need to be imported with import statements, Pyforest will automatically import them for you.

The library is imported only after it is called in your code or its objects are created. If a library is not being used or called, Pyforest will not import it.

/ 02 / use

Install Pyforest using the following command.

pip install pyforest -i https://pypi.tuna.tsinghua.edu.cn/simple
Copy the code

After the installation is successful, import it using import statements.

You can now use the relevant Python libraries directly without having to write an import.

Using Jupiter Notebook as an example, we didn’t import the Pandas, Seaborn, and Matplotlib libraries, but we can use them directly by importing the Pyforest library.

Read the data, this is the domestic cotton output of the top three provinces, Xinjiang first (data source: National Bureau of Statistics).

So can Pyforest import all libraries?

This package currently contains most popular Python libraries, such as

pandas as pd
NumPy as np
matplotlob.pyplot as plt
seaborn as sns 
Copy the code

In addition to these libraries, it also provides several auxiliary Python libraries, such as OS, TQDM, RE, and so on.

If you want to see a list of libraries, you can use dir(PyForest) to do so. There are 68 libraries built in.

import pyforest print(len(dir(pyforest))) for i in dir(pyforest): print(i) ------------------------- 68 GradientBoostingClassifier GradientBoostingRegressor LazyImport OneHotEncoder Path  RandomForestClassifier RandomForestRegressor SparkContext TSNE TfidfVectorizer...Copy the code

If not, you can make a custom addition by writing import statements to files in the home directory.

The following is an example.

vim ~/.pyforest/user_imports.py
Copy the code

Add the statement, where you can use the Requests library in your code.

# Add your imports here, line by line
# e.g
# import pandas as pd
# from pathlib import Path
# import re

import requests as req
~                                                                               
~                                                                                                                                                                                                      
"~/.pyforest/user_imports.py" 7L, 129C
Copy the code

Let’s try it this time in PyCharm.

After PyCharm’s auto-complete function was disabled, it seems that the library is suitable for Jupyter Notebook (the auto-complete code still works).

In addition to the above location, you can also add them in the _import.py file of the library.

Here Pyechars is used as an example, which is abbreviated as Chart.

The visual code is shown below.

Cotton production in Xinjiang rose year after year, other provinces fell year after year…

Finally, Pyforest provides some functions to see how the library is being used.

# returns have been imported and are using library list print (pyforest. Active_imports ()) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- [' import pandas as pd, 'import requests as req', # 'import pyg2plot'] returns a list of all the Python library pyforest print (pyforest. Lazy_imports ()) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- [' import glob', 'import numpy as np', 'import matplotlib.pyplot as plt'...]Copy the code

Only the library used in the code will be imported into the program, otherwise it will not import oh!

/ 03 / Summary

Well, that’s the end of sharing this issue.

Using the Pyforest library can sometimes be a time saver, but there are drawbacks.

For example, when debugging (large projects), it can be painful not to know where the library came from.

Therefore, it is recommended that you use it in some independent scripts, and the effect should be good.