python - 使目录成为可导入包的最佳做法是什么?
问题描述
我创建了一些实用函数,都放在一个文件夹中,后来我创建了一些脚本来导入该文件夹中的函数,请参阅以下文件结构,
root
|-- myutils (folder with util python files)
| |-- __init__.py (empty)
| |-- utils1.py
| |-- utils2.py (utils1 is imported by utils2)
|
|-- myscripts
| |-- hello.py (try to import utils1 and utils2)
现在的问题是我无法在 hello.py 中导入 utils1/utils2,这是我尝试过的
# This is hello.py
import sys
sys.path.append("root") # so that we can search root and find myutils
import myutils # import myutils, this is OK and no errors
x = myutils.utils1.foo() # ERROR: module 'myutils' has no attribute 'utils1'
我该如何解决这个错误,我的用例的最佳实践是什么?
解决方案
推荐阅读
- python - 我收到 InvalidArgumentError 因为索引显然超出范围。为什么?
- angular - 有人可以分享有关修复此错误的任何见解吗?
- graphql - 取消 GraphQL 订阅
- java - 如何返回以特定项目开头的 Java 项目列表的组合列表?
- python - 在 pandas 数据框中添加一列,该列是基于其他列条件的另一列的平均值
- ssl - 如何使用 Socket.io 通过 SSL 连接?
- laravel - Laravel 和害虫 PHP | Github 行动 | cURL 错误 7:无法连接到 localhost 端口 8000
- python - 如何按顺序加载 MNIST 数据进行训练?
- ssl - 使用 wsimport 创建 SOAP 客户端,但无法使其与 SSL 证书一起使用
- node.js - 在node js中处理多个api调用同步方式