导入python包的正确方法是什么?

时间:2014-01-03 17:19:14

标签: python testing module path operating-system

我现在以下列方式构建了一个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目录上的~/home目录是git仓库:/home/pribeiro/foo 我还试图做import foo

import foo
ImportError: No module named foo

1 个答案:

答案 0 :(得分:0)

你的PATH中有foo吗? 尝试

import sys
print sys.path

如果将foo添加到路径中,foo.bar.src应该是一个有效的模块

相关问题