如何仅导入包中没有exec __init__.py的子模块 [英] How to only import sub module without exec __init__.py in the package
问题描述
从包中导入子模块时,包文件夹中的__init__.py文件将首先执行,我该如何禁用它.有时我只需要一个功能包,导入整个程序包就很麻烦.
When import sub module from a package, the __init__.py file in the package folder will be exec first, how can I disable this. Sometimes I only need one function in a package, import the whole package is a little heavy.
例如, pandas.io.clipboard
模块不依赖于pandas中的任何其他功能.
For example the pandas.io.clipboard
module doesn't dependent on any other functions in pandas.
clipboard_get
将导入函数,但也将导入所有pandas通用模块.有什么方法可以导入剪贴板模块,因为它是我自己的应用程序文件夹中的模块.
from pandas.io.clipboard import clipboard_get
will import the function, but also import all the pandas common modules. Are there some method that just import the clipboard module, as it's a module in my own application folder.
推荐答案
没有,根据设计.如果要避免导入子模块时的大量开销,则只需使用空的 __ init __.py
s来定义软件包.这样,导入软件包的开销几乎为零.
No there isn't, by design. If you want to avoid much overhead when import sub-modules you just use empty __init__.py
s to define the packages. In this way the overhead of importing the package is practically zero.
如果 pandas
不执行 ,则您可以 no 的方式来导入 pandas.io.clipboard
而不进行导入首先是 pandas
和 io
.您可以进行的操作,但这是一个巨大的 hack ,并且不等效,是导入 clipboard
模块作为普通模块而不是子模块.您只需要找到 pandas
的安装位置(例如/usr/lib/pythonX.Y/dist-packages/
),然后将父包的路径插入 sys.path
(在您的情况下为/usr/lib/pythonX.Y/dist-packages/pandas/io
).然后,您可以通过执行以下操作导入 clipboard
包:
If pandas
does not do that you have no way to import pandas.io.clipboard
without importing pandas
and io
first. What you can do, however it's a huge hack and it is not equivalent, is to import the clipboard
module as a normal module instead of as a sub-module. You simply have to find the location where pandas
is installed (e.g. /usr/lib/pythonX.Y/dist-packages/
) and insert the path of the parent package in the sys.path
(/usr/lib/pythonX.Y/dist-packages/pandas/io
in your case). Then you can import the clipboard
package by doing:
import clipboard
但是请注意:
import clipboard
from pandas.io import clipboard as clipboard2
print(clipboard == clipboard2)
将打印 False
.实际上,这样做会破坏很多代码,因为从根本上破坏了 import
机制所假定的一些不变量.
Will print False
. In fact doing this can break a lot of code, since you are fundamentally breaking some invariants that the import
mechanism assumes.
尤其是如果子模块确实使用相对导入引用其他子模块,则导入将失败,并且在其他情况下,导入将无法正确执行.失败的另一个示例是是否必须处理腌制的对象.如果使用导入为 pandas.io.clipboard
的模块对某些对象进行腌制,您将不能使用导入的 clipboard
的模块来腌制它们.如上所述.
In particular if the sub-module does reference other sub-modules using relative imports the import will fail, and there are other situations where it will not behave correctly. An other example where this fails is if you have to deal with pickled objects. If you have some objects pickled using the module imported as pandas.io.clipboard
you will not be able to unpickle them using the module clipboard
imported as above.
总而言之,不要!我建议:
- 如果导入软件包所花费的时间不是 real 问题,则可以忍受.
- 或:尝试搜索替换项.如果您只需要
pandas.io.clipboard
,而不需要其余的pandas
,则也许您不应该首先使用pandas
,而应该使用较小的程序包,该程序包仅实现clipboard
的功能.
- Live with it if the time taken to import the package it's not a real issue.
- Or: Try to search for a replacement. If you need only
pandas.io.clipboard
but not the rest ofpandas
maybe you shouldn't usepandas
in the first place and you should use smaller package that implements only the functionality ofclipboard
.
如果您查看 pandas.util.clipboard
源代码,您发现它实际上只是 pyperclip
模块版本1.7.您可以只在 site-packages
中添加此模块,然后使用它代替 pandas
提供的模块.实际上, pandas
团队仅在源代码末尾添加了以下内容:
If you look at pandas.util.clipboard
source code you find out that it's actually just the pyperclip
module version 1.7. You can just add this module in your site-packages
and use it instead of the one provided by pandas
. In fact the pandas
team only added the following piece at the end of the source code:
## pandas aliases
clipboard_get = paste
clipboard_set = copy
对为什么 python导入进行一些扩展,以这种方式进行.
Expanding a bit about why python import works this way.
您在python中知道模块是对象.而且,虽然不是每个模块都是包,但 packages是模块也会发生.当您按照以下方式导入软件包时:
As you know in python modules are objects. And it also happens that packages are modules, although not every module is a package. When you import a package as in:
import pandas.io.clipboard
Python必须:
- 创建
module
实例pandas
- 创建
module
实例io
并将其作为属性添加到pandas
- 创建
module
实例clipboard
,并将其作为属性添加到io
.
- Create the
module
instancepandas
- Create the
module
instanceio
and add it as attribute topandas
- Create the
module
instanceclipboard
and add it as attribute toio
.
为了创建 module
实例,python 必须在模块中执行代码.
In order to create a module
instance python must execute the code in the module.
导入的形式:
from pandas.io import clipboard
仅仅是语法糖:
import pandas.io.clipboard
clipboard = pandas.io.clipboard
del pandas.io
请注意,在 from
情况下, clipboard
可以是 module
/package,也可以只是在 io
.为了对此进行检查,解释器必须还要导入 io
并为此进行 must 还要导入 pandas
.
Note that in the from
case clipboard
could be either a module
/package or simply something defined inside io
. In order to check for this the interpreter must also import io
and to do this it must also import pandas
.
这篇关于如何仅导入包中没有exec __init__.py的子模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!