【发布时间】:2018-03-16 20:39:21
【问题描述】:
这可能是重复,但无论如何都与 python 中的导入直接相关。
我的目录结构如下:
Main /
sample.py
utils / preprocess.py , __init__.py
Data / stopwords.txt
在sample.py中
from utils import preprocess
在 preprocess.py 中
import codecs
stopwords_ = codecs.open('../Data/stopwords.txt' , encoding='utf-8')
stopwords_ = stopwords_.readlines()
现在错误是当我运行 sample.py IOError: [Errno 2] No such file or directory: '../Data/stopwords.txt' 。我理解错误的症结所在,因为当我在 preprocess.py 中打印 os.getcwd() 时,我得到了 '/home/username/Main' 。
但是如何解决它。任何帮助将不胜感激
【问题讨论】:
-
在路径中使用“Data/stopwords.txt”
-
@planet260 - 我猜这不是一个好方法。我正在寻找标准解决方案。谢谢。
标签: python import relative-path