【发布时间】:2014-01-03 17:19:14
【问题描述】:
到目前为止,我有一个 Python 项目,其结构如下:
foo/
__init__.py
bar/
__init__.py
config.cfg
README.md
src/
__init__.py
myfile_a.py
myfile_b.py
tests/
test_myfile_a.py
README.md
在test_myfile_a.py 里面我有:
1 #!/usr/bin/python
2 import unittest
3 from foo.bar.src import myfile_a
4
5 class TestToolsFunctions(unittest.TestCase):
6
7 def setUp(self):
8 file_a = MyFile_A()
9 if __name__ == '__main__':
10 unittest.main()
然后我得到:
Traceback (most recent call last):
File "tests/test_myfile_a.py", line 3, in <module>
from foo.bar.src import myfile_a
ImportError: No module named foo.bar.src
设置该路径的正确方法是什么?
** 编辑 **
foo 目录是一个 git 存储库,位于我的 ~/home 目录:/home/pribeiro/foo
我也尝试过import foo:
import foo
ImportError: No module named foo
【问题讨论】:
-
foo在哪里?import foo有效吗? -
我在页面 RHS 的“相关”列表中看到至少 3 个问题可能会为您提供问题的答案。
-
如果
import foo不起作用,您可能必须先import sys然后sys.path.append(<Directory>)才能将其添加到您的路径中。 -
@chepner 问题已更新...
-
@JonathanLeffler 不,他们帮不了我……我之前做过搜索……
标签: python testing module path operating-system