简体   繁体   English

Python库项目结构最佳实践:导入和测试

[英]Python library project structure best practice: imports and tests

I want to refactor a python library i am using a lot in my day to day work to publish it on github as open source. 我想重构一个python库,我在我的日常工作中使用很多,在github上将它作为开源发布。 Before doing so, i would like to be compliant with some kind of best practice for python projects structure. 在这样做之前,我希望符合某种python项目结构的最佳实践。 I am going to describe below want i would like to do and i would appreciate your suggestions. 我将在下面描述想要我想做什么,我将不胜感激您的建议。

Here is my library (mylib) structure: 这是我的库(mylib)结构:

mylib/
   /examples/
       simple_example.py
   /mylib/
       __init__.py
       foo.py
       bar.py
   /tests/
       test_foo.py
       test_bar.py

Here are the files: 这是文件:

#foo.py
def Foo():
    print("foo.Foo")


#bar.py
import foo

def Bar():
    print("bar.Bar")
    foo.Foo()


#test_bar.py
from ..mylib import bar #doesnt work!

class TestBar(unittest.TestCase):
    def test_1(self):
        bar.Bar()
        self.assertEqual(True, True)

if __name__ == '__main__':
    unittest.main()


#simple_example.py
from .. import foo #doesnt work!
from .. import bar #doesnt work!

if __name__ == '__main__':  
    foo.Foo()
    bar.Bar()

What i would like to do is: 我想做的是:

1- Execute simple_example.py ideally from /mylib/examples/: 1-理想情况下从/ mylib / examples /执行simple_example.py:

$cd myapp
$cd examples
$python simple_example.py
Traceback (most recent call last):
  File "simple_example.py", line 2, in <module>
    from .. import foo
SystemError: Parent module '' not loaded, cannot perform relative import

2- Execute a single test file ideally from /mylib/tests/: 2-理想情况下从/ mylib / tests /执行单个测试文件:

$cd myapp
$cd tests
$python test_bar.py
Traceback (most recent call last):
  File "test_bar.py", line 3, in <module>
    from ..mylib import bar
SystemError: Parent module '' not loaded, cannot perform relative import    

3- Execute all tests from mylib root 3-从mylib root执行所有测试

$cd myapp
$python -m unittest discover tests #same problem as above!

So, the problems are in the import statement in simple_example.py and test_bar.py. 因此,问题出在simple_example.py和test_bar.py中的import语句中。 What is the best method to fix those imports? 修复这些进口的最佳方法是什么?

Note that I would like to use python standard lib unittest for unit testing. 请注意,我想使用python标准lib unittest进行单元测试。

Thanks 谢谢

Charlie 查理

When running your test code, you want to do absolute imports. 运行测试代码时,您希望进行绝对导入。 This is because when you're running unit tests, etc., you should assume your 'library' is installed in local development mode for testing -- don't use relative imports because you are not in the same package. 这是因为当您运行单元测试等时,您应该假设您的“库”以本地开发模式安装以进行测试 - 不要使用相对导入,因为您不在同一个包中。

Here's how you would do an import in your test_foo.py file, for example: 以下是在test_foo.py文件中导入的test_foo.py ,例如:

# test_foo.py
from mylib.foo import Foo

# ... your test code here

In general, you should only use relative imports INSIDE of your library code, not in your tests =) 通常,您应该只使用库代码中的相对导入INSIDE,而不是在您的测试中使用=)

I hope this helps. 我希望这有帮助。

EDIT : You also need to install your library in development mode before this will work. 编辑 :您还需要在开发模式下安装库,然后才能工作。 You can do this in one of two ways: 您可以通过以下两种方式之一完成此操作:

$ python setup.py develop

OR 要么

$ pip install -e .

Either of the above commands will inspect your project's setup.py file, which tells Python how your package is built / created, and will install it locally so you can run tests / mess with it. 上述任一命令都将检查项目的setup.py文件,该文件告诉Python如何构建/创建包,并将其本地安装,以便您可以运行测试/混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM