Python imports for tests using nose - what is best practice for imports of modules above current package

让人想犯罪 __ 提交于 2019-11-27 17:56:44

You have answered your question pretty well already.. D (install to system location) is preferred for distributable code. I usually use C (modify sys.path) because I don't want system-wide installs of my hundreds of custom libs. In theory A (relative import) seems nicer, but there are cases where it fails. B (PYTHONPATH) is right out, really only for testing purposes in my opinion.

That pretty much sums up all of the options. The option you prefer (Python magically knows where to look) is really not a workable solution because it can lead to unpredictable results, such as automagically finding libraries from unrelated projects.

In my opinion, the best thing to do is put this at the entry point(s) to your program:

import sys, os
sys.path = [os.path.abspath(os.path.dirname(__file__))] + sys.path
cnu

I had the same problem and found an answer in a related question work for me.

Just remove the __init__.py in the project root.

I know there is a answer checked and I still think it's a good reason to share other alternatives :)

There is a nose-pathmunge giving you a control to set sys.path while invoking nosestests.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!