简体   繁体   English

创建Python包并导入模块

[英]create Python package and import modules

I'm trying to write my first Python package, and almost all my modules will need to use NumPy. 我正在尝试编写我的第一个Python包,几乎所有模块都需要使用NumPy。 Should I write import numpy in every single module or is there some place in the package I can just import it once so every module can use it? 我应该在每个模块中编写import numpy还是在包中有一些地方我可以只导入一次,这样每个模块都可以使用它? What's the best way to do this? 最好的方法是什么?

Yes, just import it everywhere it's needed. 是的,只需在任何需要的地方导入它。

Don't get too clever with writing functions that import everything for you or metamodules that import things and from which you import * ; 编写为您导入所有内容的函数或导入内容并从中import *模块时, 不要太聪明; all of that only serves to make your code hard to read and is just another place for bugs to happen. 所有这一切只会使您的代码难以阅读,而且只是发生错误的另一个地方。

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

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