python - 为什么我无法在我的工作目录中导入模块?
问题描述
所以我有一堆我正在尝试运行的代码。
一方面,我设置了我的工作目录并执行了存储在那里的代码。
cd /Users/abrahammathew/Desktop/object_detection_cats/object_detection/
python3 export_inference_graph.py \
--input_type image_tensor \
--pipeline_config_path data/ssd_mobilenet_v1_pets.config \
--trained_checkpoint_prefix data/model.ckpt-997 \
--output_directory object_detection_graph
但是,这会产生此错误。
from object_detection import exporter
Traceback (most recent call last):
File "<ipython-input-20-0bc5d13491d6>", line 1, in <module>
from object_detection import exporter
ImportError: cannot import name 'exporter'
这没有任何意义,因为导出器文件位于工作目录中。
为什么导入命令不起作用?
我去尝试通过设置 os.chdir 然后导入将其导入 Spyder,但这也会导致错误。
os.chdir('/Users/abrahammathew/Desktop/object_detection_cats/object_detection')
import exporter ### THIS WORKS!!!!!
from object_detection import exporter
Traceback (most recent call last):
File "<ipython-input-23-0bc5d13491d6>", line 1, in <module>
from object_detection import exporter
ImportError: cannot import name 'exporter'
解决方案
工作目录在object_detection
目录内。为了import object_detection
工作,你需要在父目录中,/Users/abrahammathew/Desktop/object_detection_cats
. 所以要么:
- 在父目录中工作(对于旧版本的 Python,您可能需要在 中设置一个空
__init__.py
,object_detection
但现代版本使用隐式命名空间包,因此__init__.py
不需要),或者 - 将导入更改为just
import exporter
(它本身就是工作目录中的一个模块)。
推荐阅读
- vba - 基于两个参数导出 Excel 表格的单元格值
- haskell - Instance Eq 在 Haskell 中不起作用
- python - 转置卷积如何在张量流中使用相同的填充
- reactjs - 如何使用 yarn 在 vsts 中创建反应构建管道。找不到 package.json
- amazon-web-services - 如何定义过滤器表达式以根据 aws app 同步查询过滤器中的对象键值过滤嵌套的 json 数组?
- reactjs - react-sortable-hoc 在第一次使用 edgeOffset 移动后停止
- c# - FileHelpers 删除列表/重置
- bash - 在 lftp 中添加 shell if 语句
- javascript - 如何在javascript中转换为sql数据
- multithreading - 在多线程场景中使用 Redemption GetMessageFromID