Lazy Gospel! One line of code to import all Python Libraries

Recently, when consulting some questions, some small partners found that many small partners did not know "opportunism" in the process of practical operation. That is the so-called "laziness". The laziness mentioned here is not the traditional laziness and trickery, but learning to "use tools". You should know that learning more often needs to stand on the shoulders of giants to climb higher. Many things don't need you to spend more energy on making parts. Predecessors have built "wheels" ~ use them directly and get twice the result with half the effort!

Today I'd like to introduce a lazy Python Library - Pyforest.

With one line of code, you can import all Python libraries (locally installed).

GitHub address: https://github.com/8080labs/pyforest

1, Introduction to pyforest

Python is very popular because it has thousands of powerful open source libraries. At present, more than 235000 Python libraries can be imported through PyPl, with a huge number. In our daily practice, we usually need to import multiple libraries or frameworks to perform tasks. And whenever you create a new program file, you need to import relevant libraries according to your own needs. If it is the same type of task, such as a small data visualization project, it may be used until a library. In this way, if you repeatedly write the same import statement, you will feel trouble even if you copy and paste. At this time, the pyforest library can play. Pyforest is an open source Python library that can automatically import Python libraries used in code. During data visualization, you generally need 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 the exact import statement.

For example, the following line of code can be omitted.

from sklearn.ensemble import RandomForestClassifier

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

import pyforest

df = pd.read_csv('test.csv')
print(df)

Any library you use does not need to be imported with the import statement. Pyforest will import it automatically for you.

The library will be imported only after calling the library in the code or creating the object of the library. If a library is not used or called, Pyforest will not import it.

2, How to use pyforest

1. To install, use the following command to install Pyforest.

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

2. Import using the import statement

Now, you can use the relevant Python libraries directly without writing import.

First, take jupiter notebook as an example. We did not import pandas, seaborn and matplotlib libraries, but we can use them directly by importing Pyforest libraries.

3. Read the data, this is the top three provinces in domestic cotton production, and Xinjiang is the first in China (data source: National Bureau of Statistics).

Can Pyforest import all libraries?

At present, this package contains most of the popular Python libraries, such as

pandas as pd
NumPy as np
matplotlob.pyplot as plt
seaborn as sns 

In addition to these libraries, it also provides some auxiliary Python libraries, such as os, tqdm, re, etc.

If you want to view the library list, you can use dir(pyforest) to view it. There are 68 built-in libraries.

import pyforest

print(len(dir(pyforest)))
for i in dir(pyforest):
    print(i)

-------------------------
68
GradientBoostingClassifier
GradientBoostingRegressor
LazyImport
OneHotEncoder
Path
RandomForestClassifier
RandomForestRegressor
SparkContext
TSNE
TfidfVectorizer

If not, you can add it by yourself and write the import statement to the file in the home directory.

Examples are as follows:

vim ~/.pyforest/user_imports.py

Add a 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

This time, let's experiment in PyCharm.

It is found that PyCharm's automatic completion function fails. It seems that this library is more suitable for jupyter notebook (the automatic completion code can also be used).

In addition to the above, you can customize and add them in the library_ import.py file.

Here, take Pyechars as an example, abbreviated as chart.

The visualization code is as follows:

Cotton production in Xinjiang increases year by year and decreases year by year in other provinces

Finally, Pyforest also provides some functions to understand the usage of the library.

#Returns a list of libraries that have been imported and are in use
print(pyforest.active_imports())
--------------------------------
['import pandas as pd', 'import requests as req', 'import pyg2plot']


#Returns a list of all Python libraries in pyforest
print(pyforest.lazy_imports())
--------------------------------
['import glob', 'import numpy as np', 'import matplotlib.pyplot as plt'...]

Only when there is a library used in the code, the program will be imported, otherwise it will not be imported!

3, Summary

Things always have advantages and disadvantages. Sometimes using Pyforest library can save some time, but there are disadvantages. For example, when debugging (large projects), it may be very painful. I don't know where the library came from.

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

That's all for today's tips. We'll share more dry goods tips in the future~

If you have any questions during the actual operation, you can enter the group for consultation. For friends at all stages, you will find the answer you want here ~ communication group: 954526228 (note that the code for entering the group is: Xiaoyu)

There is also a full set of information suitable for novices of Xiaobai~

Come and grow and progress with little fish!

① More than 2000 Python e-books (both mainstream and classic books should be available)

② Python standard library materials (the most complete Chinese version)

③ Project source code (forty or fifty interesting and classic hand training projects and source code)

④ Videos on basic introduction to Python, crawler, web development and big data analysis (suitable for Xiaobai)

⑤ Python learning roadmap (bid farewell to non stream learning)

Keywords: Python Back-end crawler

Added by leon_nerd on Thu, 20 Jan 2022 22:59:44 +0200