[英]Hide imports within another package python
I use Jupyter a lot, and while using jupyter i have the same list of imports that are long and cumbersome, something like:我经常使用 Jupyter,在使用 jupyter 时,我有相同的导入列表,这些列表又长又麻烦,例如:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from IPython.core.display import display, HTML
from ipywidgets import interact, IntSlider
from IPython.display import display
pd.options.display.max_columns = 35
pd.options.display.max_rows = 300
plt.rcParams['figure.figsize'] = [12, 8]
plt.rcParams['figure.dpi'] = 170 # 200 e.g. is really fine, but slower
import IPython.display as ipd
plt.ion()
display(HTML("<style>.container { width:95% !important; }</style>"))
def plot_frozen(df, num_rows=30, num_columns=30, step_rows=1,
step_columns=1):
"""
Freeze the headers (column and index names) of a Pandas DataFrame. A widget
enables to slide through the rows and columns.
Parameters
----------
df : Pandas DataFrame
DataFrame to display
num_rows : int, optional
Number of rows to display
num_columns : int, optional
Number of columns to display
step_rows : int, optional
Step in the rows
step_columns : int, optional
Step in the columns
Returns
-------
Displays the DataFrame with the widget
"""
@interact(last_row=IntSlider(min=min(num_rows, df.shape[0]),
max=df.shape[0],
step=step_rows,
description='rows',
readout=False,
disabled=False,
continuous_update=True,
orientation='horizontal',
slider_color='purple'),
last_column=IntSlider(min=min(num_columns, df.shape[1]),
max=df.shape[1],
step=step_columns,
description='columns',
readout=False,
disabled=False,
continuous_update=True,
orientation='horizontal',
slider_color='purple'))
def _freeze_header(last_row, last_column):
display(df.iloc[max(0, last_row-num_rows):last_row,
max(0, last_column-num_columns):last_column])
It's imports and a bunch of plotting/display helper functions.它是导入和一堆绘图/显示辅助函数。
Is there a way for me to bundle all of this up into a single pip
package so that i can only have a line or two?有没有办法将所有这些捆绑到一个pip
package 中,这样我就只能有一两行?
I'm imagining running:我想象着跑步:
pip install Genesis
then inside my jupyter notebook have:然后在我的 jupyter notebook 里面有:
import Genesis
and nothing else.没有别的。
I've tried making a genesis package that is basically a copy of this guide but with a single file called jupyter.py
that contains the setup code above.我已经尝试制作一个 genesis package,它基本上是本指南的副本,但有一个名为jupyter.py
的文件,其中包含上面的设置代码。
Then I run the following:然后我运行以下命令:
from Genesis import jupyter
jupyter.setup()
But it doesn't import pandas
, numpy
and matplotlib.pyplot
for me.但它不会为我导入pandas
、 numpy
和matplotlib.pyplot
。 It makes sense because those packages are imported within the scope of the package. But any way to avoid that?这是有道理的,因为这些包是在 package 的 scope 中导入的。但是有什么办法可以避免这种情况呢? Is it even possible in Python? Python 有可能吗?
You can make a package with all your imports no problem, you just need to be careful of namespaces.你可以用你所有的导入制作一个 package 没问题,你只需要注意命名空间。
Say I have a file:假设我有一个文件:
# genesis/__init__.py
import pandas as pd
import numpy as np
...
Importing that genesis package will run that code, but it won't be accessible directly导入 genesis package 将运行该代码,但不能直接访问它
>>> import genesis
>>> help(np)
raceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'np' is not defined
>>> help(genesis.np) # This should succeed
...
You could address this with from genesis import *
which would bring everything into the namespace you expect您可以使用from genesis import *
解决此问题,这会将所有内容带入您期望的名称空间
eg例如
>>> from genesis import *
>>> help(np) # This should succeed
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.