将Python源拆分为单独的目录? [英] Split Python source into separate directories?

查看:90
本文介绍了将Python源拆分为单独的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我公司foo.com使用的各种Python软件包:

Here are some various Python packages my company "foo.com" uses:

com.foo.bar.web
com.foo.bar.lib
com.foo.zig.web
com.foo.zig.lib
com.foo.zig.lib.lib1
com.foo.zig.lib.lib2

以下是将源存储在磁盘上的传统方法:

Here's the traditional way to store the source on disk:

pysrc/
  com/
    foo/
      bar/
        web/
        lib/
      zig/
        web/
        lib/
          lib1/
          lib2/

PYTHONPATH=pysrc

但是出于组织目的(不同的团队,不同的版本控制等),我们希望将这些存储如下:

But for organizational purposes (different teams, different revision control, etc.), we want to store these as follows:

bar/
  pysrc/
    com/
      foo/
        bar/
          web/
          lib/
zig/
  pysrc/
    com/
      foo/
        zig/
          web/
          lib/
            lib1/
            lib2/

PYTHONPATH=bar/pysrc:zig/pysrc

问题是:

第二种组织方法是否有任何问题?

Are there any issues with this second method of organization?

例如,如果我们 import com.foo ,Python在哪里寻找 __ init __。py

For example, if we import com.foo, where would Python look for the __init__.py?

symlinking这些目录是否有意义?例如:

Would symlinking these directories make sense? e.g.:

pysrc/
  com/
    foo/
      bar/ -> symlink to /bar/pysrc/com/foo/
      zig/ -> symlink to /zig/pysrc/com/foo/

欢迎任何一般代码组织建议。

Any general code organizational suggestions are welcome.

推荐答案

Python将按顺序通过sys.path(包括PYTHONPATH,然后是一些),在每个中查找一个com.foo包。它找到的第一个,它将用于排除其他人,不像Perl或Java有效地将包名称空间合并在一起。您可以对 __ path __ 执行更改此行为的操作,但首次匹配获胜是Python开箱即用的行为。

Python will go through sys.path in order (which includes PYTHONPATH and then some), looking for a com.foo package in each. The first one it finds, it will use to the exclusion of others, unlike Perl or Java which effectively merges together package namespaces. There are things you can do to __path__ that change this behavior, but "first match wins" is how Python behaves out of the box.

只要你将所有com.foo.bar完全保存在bar /并且所有com.foo.zig完全保存在zig /中,你就不应该对第二个布局有任何问题。

As long as you keep all of com.foo.bar entirely in bar/ and all of com.foo.zig entirely in zig/, you shouldn't have any problems with the second layout.

这篇关于将Python源拆分为单独的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆