创建Python包并导入模块 [英] create Python package and import modules

查看:90
本文介绍了创建Python包并导入模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写我的第一个Python包,几乎所有模块都需要使用NumPy.我应该在每个模块中写import numpy还是在软件包中有什么地方可以导入一次,以便每个模块都可以使用?最好的方法是什么?

I'm trying to write my first Python package, and almost all my modules will need to use 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? What's the best way to do this?

推荐答案

是的,只需将其导入所需的任何地方即可.

Yes, just import it everywhere it's needed.

不要太聪明了,编写的函数可以为您导入所有内容,或者元模块可以为您导入内容,并且您可以从中import *;所有这些只会使您的代码难以阅读,而这正是发生错误的另一个地方.

Don't get too clever with writing functions that import everything for you or metamodules that import things and from which you import *; all of that only serves to make your code hard to read and is just another place for bugs to happen.

这篇关于创建Python包并导入模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆