简体   繁体   English

在单独的.py文件中编写导入模块列表

[英]Writing list of import modules in a separate .py file

I have a long list of import modules, something like this: 我有一长串导入模块,如下所示:

import x
import y
.
.

How can I write these all imports in one line? 如何将所有这些导入内容写在一行中? I come from C programming and see people doing #include "all_lib.h". 我来自C编程,看到人们在做#include“ all_lib.h”。 Is there a equivalent or better pythonic way of doing this ? 有等效或更好的pythonic方式吗?

Thanks. 谢谢。

You can put all those imports in one python file, call it all_imports.py , and then import everything from that file using from all_imports import * in your actual Python file. 您可以将所有这些导入放入一个python文件中,将其all_imports.py ,然后在实际的Python文件中使用from all_imports import *该文件中的所有内容。

Example: 例:

File all_imports.py 文件all_imports.py

import math
import collections

File test.py 文件test.py

from all_imports import *

print dir()
print math.pi

您只有一个文件,例如包含所有导入模块的myimports.py,然后将其用作from myimports import *

You can use from all_imports import * . 您可以from all_imports import * But using names not explicitly named in the module would not be pythonic according to the Style Guide for Python Code (under wildcards). 但是根据Python代码样式指南 (在通配符下),使用未在模块中显式命名的名称不是pythonic。

Actually, you can do 其实你可以

import x, y

But you shouldn't do it according to PEP 8 但您不应该按照PEP 8进行操作

you can simply do: 您可以简单地执行以下操作:

import a, b, c, d, e, f, g, h

But take a look at the Style Guide for Python Code which says that you shall put them on separate lines. 但是请看一下Python代码样式指南 ,该指南指出您应该将它们放在单独的行中。

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

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