包装组织:在哪里放置'通用'模块? [英] Package organization: where to put 'common' modules?
问题描述
假设我在系统中有以下包裹组织我是
开发:
A
| - --- B
| ---- C
| ---- D
我有一个模块,比如说''foo'',包D和B都需要。什么是
创建普通套餐的最佳做法是什么?
''foo''?我希望能够支付
- 在D的目录中测试D中的模块,并在B'目录中的B / /
中测试模块;
- 如果可能的话,尽量减少对PYTHONPATH或sys.path
的修改。
有可能吗?
谢谢!
Say I have the following package organization in a system I''m
developing:
A
|----B
|----C
|----D
I have a module, say ''foo'', that both package D and B require. What is
the best practice in terms of creating a ''common'' package that hosts
''foo''? I want to be able to
- Testing modules in D right in D''s directory and testing modules in B
in B''s directory;
- If possible, minimize the modification to PYTHONPATH or sys.path
while doing the above.
Is it possible?
Thanks!
推荐答案
好的我想多一点预感可能需要引出建议。
如果我选择将foo放入A中,是否有办法让B中的代码从A导入foo
?也就是说,有没有办法从父母
目录/包中导入东西?
如果不改变sys.path就不可能'是
最小努力的路径 - 即,需要最少量代码的方式
(在B下的每个模块中添加代码以预先添加sys.path这个技巧是什么?但是需要很多代码。
谢谢!
Ok I guess a little more "hunch" might be needed to elicit suggestions.
If I chose to put foo in A, is there a way for code in B to import foo
from A? Namely, is there a way to import stuff from a parent
directory/package?
If it''s not possible without changing sys.path, what''s the path of
least effort - i.e., the way that requires least amount of added code
(adding code to prepend sys.path in every module under B certain does
the trick but requires a lot code).
Thanks!
fortepianissimo写道:
fortepianissimo wrote:
说我在一个系统中有以下包组织我正在开发:
A
| ---- B
| ---- C
| ---- D
我有一个模块,比如''foo'',包D和B都需要。什么是创建一个主持
'foo''''常见''包的最佳实践?我希望能够在D'的目录中测试D中的模块,并在B'目录中的B
中测试模块;
- 如果可能的话,在执行上述操作时,最小化对PYTHONPATH或sys.path的修改。
Say I have the following package organization in a system I''m
developing:
A
|----B
|----C
|----D
I have a module, say ''foo'', that both package D and B require. What is
the best practice in terms of creating a ''common'' package that hosts
''foo''? I want to be able to
- Testing modules in D right in D''s directory and testing modules in B
in B''s directory;
- If possible, minimize the modification to PYTHONPATH or sys.path
while doing the above.
我所做的是始终从基本目录运行(违反您的第一个
要求)。我创建一个util包来保存常用的代码。然后B
和D都使用
来自util import foo
在Python 2.5中你可以说(在D中) ,例如)
来自..util import foo
http://www.python.org/peps/pep-0328.html
Kent
What I do is run always from the base directory (violates your first
requirement). I make a util package to hold commonly used code. Then B
and D both use
from util import foo
In Python 2.5 you will be able to say (in D, for example)
from ..util import foo
http://www.python.org/peps/pep-0328.html
Kent
Kent Johnson< ke ** @ kentsjohnson.com>写道:
Kent Johnson <ke**@kentsjohnson.com> writes:
我所做的是始终从基本目录运行(违反了您的第一个要求)。我创建一个util包来保存常用的代码。然后B和D
都使用来自util import foo的
在Python 2.5中你可以说(例如在D中)来自..util import foo
http:// www。 python.org/peps/pep-0328.html
What I do is run always from the base directory (violates your first
requirement). I make a util package to hold commonly used code. Then B and D
both use
from util import foo
In Python 2.5 you will be able to say (in D, for example)
from ..util import foo
http://www.python.org/peps/pep-0328.html
我的工作方式有点不同。当两个程序开始共享代码时,我将
将此代码转换为库,即我将其设为模块,将其放入
PYTHONPATH然后导入它。可能有两个以上的项目会使用它如果
其中两个已经是:-)
-
Jorge Godoy< go ** *@ieee.org>
Quidquid latine dictum sit,altum sonatur。
- Qualquer coisa dita em latim soa profundo。
- 用拉丁语说的任何东西听起来很聪明。
I do work a bit different here. When two programs start sharing code, then I
convert this code into a "library", i.e., I make it a module, put it in
PYTHONPATH and then import it. Probably more than two projects will use it if
two of them already are :-)
--
Jorge Godoy <go***@ieee.org>
"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
这篇关于包装组织:在哪里放置'通用'模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!