【问题标题】:How to access functions from another python file in a sub folder?如何从子文件夹中的另一个 python 文件访问函数?
【发布时间】:2021-08-24 13:20:56
【问题描述】:

我的文件夹结构是,

├── handler
│   ├── s3_handler.py
│   
└── service
    ├── service.py

s3_handler 文件包含许多我需要在 service.py 中使用的函数

我在尝试

from handlers.s3_handler import *

但是有一个错误。

Traceback (most recent call last):
File "field_mapping_util.py", line 3, in <module>
  from handlers.s3_handler import * 
ModuleNotFoundError: No module named 'handlers'

如何解决这个问题

【问题讨论】:

  • 您的文件夹结构显示handler 而不是handlers。这是您的问题的错字吗?

标签: python-3.x path


【解决方案1】:

我想通了。 在 service.py 中插入,

import sys
sys.path.append("..")
from handler.s3_handler import * 

【讨论】:

    猜你喜欢
    • 2019-11-24
    • 2018-12-28
    • 2015-07-16
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-24
    相关资源
    最近更新 更多