且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

运行python脚本时自动导入模块?

更新时间:2023-11-18 18:28:34

这是一个不好的做法脚本依赖于外部启动脚本来使其工作。执行这些导入你应该怎么做。

It's bad practice to have a script which depends on an external startup script to make it work. Doing these imports is how you should do it.

如果你通过集中导入来定期导入相当大的东西,你可以简化它在一个文件中(例如,称之为 common_imports.py ),然后从中导入所有内容(来自common_imports import * )。然后,这只会变成一条线。但我仍然希望看到显式导入。

You could simplify it if you regularly import a fairly large set of things by centralising your imports in a file (call it common_imports.py, for example), and then importing all from that (from common_imports import *). That then becomes only one line to put in. I would still prefer to see explicit imports, however.

(另一个注意事项:在中导入操作系统为os as os 完全是多余的。)

(Another note: in import os as os, the as os is entirely superfluous.)