python - 从另一个文件调用 __init__.py
问题描述
我试图了解 __init__py 是如何工作的。但我有以下问题。
我有以下目录:
myfoldername/
example/
__init__.py
sayhello.py
saybye.py
main.py
在 __init__py 文件中,我导入 sayhello.py 和 saybye.py 文件
from example.sayhello import *
from example.saybye import *
在 sayhello.py 里面我有两个函数:
def hello():
print('hello')
def hello_person(x):
print('hello ' + x)
为什么在将模块导入单独的脚本 main.py 时不能调用 sayhello.py 中的两个函数?
import example as ex
ex.hello() //prints 'hello'
ex.hello_person('John') //prints module 'example' has no attribute 'hello_person'
解决方案
虽然那不应该发生。但如果问题仍然存在,您可以使用The Zen of Python。哪个说
显式优于隐式。
因此,
from example.sayhello import hello, hello_person
推荐阅读
- python - 下载了一个jupyter笔记本,但它不会打开
- javascript - 在另一个 API 调用之前清除 api 响应数组
- c++ - 使用无序映射返回向量中元素摘要的最佳方法是什么?
- python - 在 python 中使用 selenium 跳过空文本节点
- java - 更改下载文件中的作者日期
- c++ - 如何在 macOS 上使用 gcc 而不是 clang,特别是 M1?
- java - 未记录的热点 OpenJDK 系统属性
- intellij-idea - 如何在 vscode 中使用 intellij 的代码格式化程序?
- kubernetes - CPU 份额的混乱和对 CPU 限制的需求
- javascript - 命令未执行