【发布时间】:2015-12-18 13:03:42
【问题描述】:
我正在使用库pathlib,并尝试通过以下方式构建路径。
BASE_DIR = Path(__file__).ancestor(3)
secrets_file = BASE_DIR / 'main_app' / 'settings' / 'secrets.json'
但是,在运行此程序时,我收到以下错误:
TypeError: unsupported operand type(s) for /: 'Path' and 'str'
我认为我正在遵循pathlib documentation 中定义的语法(但可能我并没有以晦涩难懂的方式)。
>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
我做错了什么?
【问题讨论】:
-
你能发一个complete example吗?很可能,您已经定义了自己的类
Path。此外,通过发布有用的调试信息,即print(Path.__module__)和import sys;print(sys.modules[Path.__module__])的输出,这个问题会得到很大改善。 -
糟糕,我已经发现了我的错误。而不是
from pathlib import Path我有from unipath import Path。