懒人必备:与import说再见

import... 

这几乎是我们写python代码时必备的开头。但实际上很多时候是重复性的工作,比如数据分析时我们会:

import pandas as pd
import sklearn
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
...

1. 解决之道

那么,有没有一种方法能够免除这些重复性的工作呢?有的!那就是pyforest库。你只需要打开Anaconda Prompt,输入以下代码:

pip install pyforest

这时,你只需要使用

from pyforest import *

就可以导入所有的库了!

2. IPython 自启动

如果你使用的是IPython,例如Jupyter Notebook 或者是 Spyder,你甚至可以直接使用,而不需要大部分的import——pyforest会把它添加到IPython的自动启动中去。

3. 哪些库支持自动导入?

找到你的库安装的位置,例如我的是:D:\Softwares\Anaconda\Lib\site-packages\pyforest,并且打开_imports.py,你就可以看到默认被导入的库了。
以下是一些示例,完整版贴在文末附录

pd = LazyImport("import pandas as pd")
np = LazyImport("import numpy as np")
dd = LazyImport("from dask import dataframe as dd")
SparkContext = LazyImport("from pyspark import SparkContext")
load_workbook = LazyImport("from openpyxl import load_workbook")
wr = LazyImport("import awswrangler as wr")

4. 个性化编辑

如果你希望一些库能够被自动导入,你可以找到pyforest库中的user_imports.py文件,来进行个性化的编辑。对于我而言,这个文件位于C:\Users\Sphaera\.pyforest\user_imports.py

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

5. 其他功能

导出所有的import语句。

active_imports()

例如你只使用了pandas这个库,他就会输出

['import pandas as pd']

6. 附录

6.1 _import.py的所有内容

from ._importable import LazyImport, _get_import_statements
from .user_specific_imports import _load_user_specific_imports


# If you are missing an import and you think it is a common import, please contribute
# via creating a pull request.
# If you contribute, we can quickly collect the 80% most frequent imports
# Before you create a pull request, PLEASE READ THE FOLLOWING:

# 0) It is always best to first create a GitHub issue before creating a pull request.
# This way you can be sure that your proposal is valid and will be integrated.

# 1) The imported name should be an unambiguous standard convention and highly specific.
# Usually, you want to use the names that are proposed in the library's documentation.
# However, there should be no or little confusion with other libraries
# Good example:
#    'import pandas as pd'
# Bad example:
#    'import dash_html_components as html'
# because 'html' is not specific enough for the dash context.
# Also, it is ambiguous with e.g. IPython.display.HTML.
# A potential resolution might be 'import dash_html_components as dhc'

# 2) General, implicit imports e.g. 'from sklearn.preprocessing import *' are not possible
# because we want to make sure that there is no accidental masking of imported names

# 3) If you disagree with the conventions or you are using rare packages, you can save
# your user-specific imports in ~/.pyforest/user_imports.py


### Data Wrangling
pd = LazyImport("import pandas as pd")

np = LazyImport("import numpy as np")

dd = LazyImport("from dask import dataframe as dd")
SparkContext = LazyImport("from pyspark import SparkContext")

load_workbook = LazyImport("from openpyxl import load_workbook")

wr = LazyImport("import awswrangler as wr")

### Data Visualization and Plotting
mpl = LazyImport("import matplotlib as mpl")
plt = LazyImport("import matplotlib.pyplot as plt")

sns = LazyImport("import seaborn as sns")

py = LazyImport("import plotly as py")
go = LazyImport("import plotly.graph_objs as go")
px = LazyImport("import plotly.express as px")

dash = LazyImport("import dash")

bokeh = LazyImport("import bokeh")

alt = LazyImport("import altair as alt")

pydot = LazyImport("import pydot")

# statistics
statistics = LazyImport("import statistics")

### Machine Learning
sklearn = LazyImport("import sklearn")
OneHotEncoder = LazyImport("from sklearn.preprocessing import OneHotEncoder")
TSNE = LazyImport("from sklearn.manifold import TSNE")
train_test_split = LazyImport("from sklearn.model_selection import train_test_split")
svm = LazyImport("from sklearn import svm")
GradientBoostingClassifier = LazyImport(
    "from sklearn.ensemble import GradientBoostingClassifier"
)
GradientBoostingRegressor = LazyImport(
    "from sklearn.ensemble import GradientBoostingRegressor"
)
RandomForestClassifier = LazyImport(
    "from sklearn.ensemble import RandomForestClassifier"
)
RandomForestRegressor = LazyImport("from sklearn.ensemble import RandomForestRegressor")

TfidfVectorizer = LazyImport(
    "from sklearn.feature_extraction.text import TfidfVectorizer"
)

# Gradient Boosting Decision Tree
xgb = LazyImport("import xgboost as xgb")
lgb = LazyImport("import lightgbm as lgb")

# TODO: add all the other most important sklearn objects
# TODO: add separate sections within machine learning viz. Classification, Regression, Error Functions, Clustering

# Deep Learning
tf = LazyImport("import tensorflow as tf")
keras = LazyImport("import keras")

# NLP
nltk = LazyImport("import nltk")
gensim = LazyImport("import gensim")
spacy = LazyImport("import spacy")
re = LazyImport("import re")

### Helper
sys = LazyImport("import sys")
os = LazyImport("import os")
re = LazyImport("import re")
glob = LazyImport("import glob")
Path = LazyImport("from pathlib import Path")

pickle = LazyImport("import pickle")

dt = LazyImport("import datetime as dt")

tqdm = LazyImport("import tqdm")


##################################################
### dont make adjustments below this line ########
##################################################


#############################
### User-specific imports ###
#############################
# You can save your own imports in ~/.pyforest/user_imports.py
# Please note: imports in ~/.pyforest/user_imports.py take precedence over the
# imports above.

_load_user_specific_imports(globals())
# don't want to blow up the namespace
del _load_user_specific_imports


# #######################################
# ### Complementary, optional imports ###
# #######################################
# # Why is this needed? Some libraries patch existing libraries
# # Please note: these imports are only executed if you already have the library installed
# # If you want to deactivate specific complementary imports, do the following:
# # - uncomment the lines which contain `.__on_import__` and the library you want to deactivate

# pandas_profiling = LazyImport("import pandas_profiling")
# pd.__on_import__(pandas_profiling)  # adds df.profile_report attribute to pd.DataFrame

# bam = LazyImport("import bamboolib as bam")
# pd.__on_import__(bam)  # adds GUI to pd.DataFrame when IPython frontend can display it


def lazy_imports():
    return _get_import_statements(globals(), was_imported=False)


def active_imports(print_statements=True):
    statements = _get_import_statements(globals(), was_imported=True)
    if print_statements:
        print("\n".join(statements))
    return statements
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容