简体   繁体   English

如何只导入__init__.py的一部分?

[英]How to just import part of __init__.py?

I have a package with some files and I'm having some importation issues. 我有一个包含一些文件的软件包,并且遇到一些导入问题。 Let's say I have the following files: 假设我有以下文件:

main.py
Lib
 ├─ __init__.py
 ├─ file1.py
 └─ file2.py

main.py : main.py

from Lib import ClassA

foo = ClassA('anything')

Lib/init.py : lib / init.py

from .file1 import ClassA
from .file2 import ClassB

file1.py : file1.py

import a_lot_of_things

class ClassA:
  pass

file2.py : file2.py

import a_lot_of_other_things

class ClassB:
  pass

This code works, but Python will also import all the other classes in the file, as the ClassB. 该代码有效,但是Python还将文件中的所有其他类作为ClassB导入。 The problem is that Python takes a lot of time importing all the libraries of file2.py which I don't wanna use. 问题在于,Python需要花费很多时间来导入所有我不想使用的file2.py库。

I know it happens because Python is running init.py and it is importing all classes, even if I ask for just one. 我知道它的发生是因为Python正在运行init.py并且它正在导入所有类,即使我只要求一个类。 But I think it should do this only if I write: 但是我认为只有在我写以下内容时,它才应该这样做:

from Lib import *

Is there a way to, inside init.py, check if I'm importing all or just one specific class to run just this file/importation? 是否可以在init.py中检查是否要导入全部或仅一个特定的类以仅运行此文件/导入?

I also tried to structure my directory this way: 我还尝试通过这种方式构建目录:

main.py
Lib
 ├─ __init__.py
 ├─ ClassA
 │   ├─ __init__.py
 │   └─ file1.py
 └─ ClassB 
     ├─ __init__.py
     └─ file2.py

So I cleaned Lib/init.py and put the importations into each init.py. 因此,我清理了Lib / init.py并将导入内容放入每个init.py中。

ClassA/init.py : ClassA / init.py

from .file1 import ClassA

ClassB/init.py : ClassB / init.py

from .file2 import ClassB

But now I need to use it this way: 但是现在我需要以这种方式使用它:

main.py : main.py

from Lib import ClassA

foo = ClassA.ClassA('anything')

And I'd like to use it directly, as I wrote before. 正如我之前写的那样,我想直接使用它。 Is there a way to do this? 有没有办法做到这一点?

If you import a module (or import something from within a module) everything that that module imports will also be imported. 如果导入模块(或从模块中导入某些内容),则该模块导入的所有内容也将被导入。

You can only avoid this by placing imports within a local context 您只能通过将导入放置在本地上下文中来避免这种情况

For example 例如

my_module:

import some_stuff

def do_stuff()
    pass

def do_some_specific_things()
    import some_specific_stuff_only_relevant_to_this_function
    pass

If you now call from my_module import do_stuff then import some_stuff will be executed, but import some_specific_stuff_only_relevant_to_this_function will not. 如果你现在打电话from my_module import do_stuff然后import some_stuff会被执行,但import some_specific_stuff_only_relevant_to_this_function不会。

By the way, structuring your code as follows does not make sense: 顺便说一下,将代码构造如下是没有意义的:

main.py
Lib
 ├─ __init__.py
 ├─ ClassA
 │   ├─ __init__.py
 │   └─ file1.py
 └─ ClassB 
     ├─ __init__.py
     └─ file2.py

A class is within a module( file1 and file2 are modules). 一个类在模块内( file1file2是模块)。 A module cannot sit within a class. 一个模块不能坐在一个类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM