python Python导入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2459300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:42:02  来源:igfitidea点击:

Python importing

pythonimport

提问by Boolean

I have a file, myfile.py, which imports Class1from file.pyand file.pycontains imports to different classes in file2.py, file3.py, file4.py.

我有一个文件,myfile.py,其中进口Class1file.pyfile.py含有进口到不同的类file2.pyfile3.pyfile4.py

In my myfile.py, can I access these classes or do I need to again import file2.py, file3.py, etc.?

在我的myfile.py.py 中,我可以访问这些类还是需要再次导入 file2.py、file3.py 等?

Does Python automatically add all the imports included in the file I imported, and can I use them automatically?

Python 是否会自动添加我导入的文件中包含的所有导入,我可以自动使用它们吗?

回答by Alex Martelli

Best practice is to import every module that defines identifiers you need, and use those identifiers as qualifiedby the module's name; I recommend using fromonly when what you're importing is a module from within a package. The question has often been discussed on SO.

最佳实践是导入定义您需要的标识符的每个模块,并使用由模块名称限定的标识符;我建议from仅当您导入的是包中的模块时才使用。这个问题经常在 SO 上讨论。

Importing a module, say moda, from many modules (say modb, modc, modd, ...) that need one or more of the identifiers modadefines, does not slow you down: moda's bytecode is loaded (and possibly build from its sources, if needed) only once, the first time modais imported anywhere, then all other imports of the module use a fast path involving a cache (a dict mapping module names to module objects that is accessible as sys.modulesin case of need... if you first import sys, of course!-).

例如moda,从需要一个或多个标识符定义的多个模块(例如modb, modc, modd, ...)导入一个模块,moda不会减慢您的速度:moda的字节码已加载(如果需要,可能会从其源构建)只有一次,第一次moda在任何地方导入,然后模块的所有其他导入使用涉及缓存的快速路径(字典将模块名称映射到模块对象,sys.modules在需要时可以访问......import sys当然,如果你是第一次!-)

回答by Mike Graham

Python doesn't automatically introduce anything into the namespace of myfile.py, but you can access everything that is in the namespaces of all the other modules.

Python 不会自动将任何内容引入 myfile.py 的命名空间,但您可以访问所有其他模块的命名空间中的所有内容。

That is to say, if in file1.py you did from file2 import SomeClassand in myfile.py you did import file1, then you can access it within myfile as file1.SomeClass. If in file1.py you did import file2and in myfile.py you did import file1, then you can access the class from within myfile as file1.file2.SomeClass. (These aren't generally the best ways to do it, especially not the second example.)

也就是说,如果您from file2 import SomeClass在 file1.py中执行了并且在 myfile.py 中执行了import file1,那么您可以在 myfile 中以file1.SomeClass. 如果您import file2在 file1.py中执行了并且在 myfile.py 中执行了import file1,那么您可以从 myfile 中以file1.file2.SomeClass. (这些通常不是最好的方法,尤其是第二个例子。)

This is easily tested.

这很容易测试。

回答by tux21b

In the myfile module, you can either do from file import ClassFromFile2or from file2 import ClassFromFile2to access ClassFromFile2, assuming that the class is also imported in file.

在 myfile 模块中,您可以执行from file import ClassFromFile2from file2 import ClassFromFile2访问 ClassFromFile2,假设该类也导入到文件中。

This technique is often used to simplify the API a bit. For example, a db.py module might import various things from the modules mysqldb, sqlalchemy and some other helpers. Than, everything can be accessed via the db module.

这种技术通常用于稍微简化 API。例如,一个 db.py 模块可能会从模块 mysqldb、sqlalchemy 和其他一些帮助程序中导入各种东西。然后,一切都可以通过 db 模块访问。

回答by M. Utku ALTINKAYA

If you are using wildcard import, yes, wildcard import actually is the way of creating new aliases in your current namespace for contents of the imported module. If not, you need to use the namespace of the module you have imported as usual.

如果您使用通配符导入,是的,通配符导入实际上是在当前命名空间中为导入模块的内容创建新别名的方式。如果没有,您需要照常使用您导入的模块的命名空间。