我如何使用python模块的多个git分支?

时间:2021-05-25 07:24:37

I want to use git to allow me to work on several features in a module I'm writing concurrently. I'm currently using SVN, with only one workspace, so I just have the workspace on my PYTHONPATH. I'm realizing this is less than ideal, so I was wondering if anyone could suggest a more 'proper' way of doing this.

我想使用git来让我在同时编写的模块中处理几个特性。我目前正在使用SVN,只有一个工作区,所以我只有PYTHONPATH上的工作区。我意识到这不太理想,所以我想知道是否有人可以建议一种更“正确”的方式来做到这一点。

Let me elaborate with a hypothetical situation: I say I have a module 'eggs', with sub-modules 'foo' and 'bar'. Components in 'bar' use code in foo, so eggs/bar/a.py may 'import eggs.foo'.

让我详细说明一个假设的情况:我说我有一个模块'egg',子模块'foo'和'bar'。 'bar'中的组件使用foo中的代码,因此eggs / bar / a.py可能会'导入eggs.foo'。

Say that 'eggs' is in a git repository. I want to try out some changes to 'foo', so I copy it. The problem is that 'import eggs.foo' in eggs/bar finds the original repository in the PYTHONPATH, so it ends up using the old 'foo' instead of my modified one.

假设'eggs'在git存储库中。我想尝试对'foo'进行一些更改,所以我复制它。问题是鸡蛋/酒吧中的'import eggs.foo'在PYTHONPATH中找到原始存储库,因此最终使用旧的'foo'而不是我修改过的'foo'。

How do I set myself up such that each copy of the module uses its own associated 'foo'? Thanks.

我如何设置自己,使模块的每个副本使用自己的关联'foo'?谢谢。

edit- Thanks for the pointer to relative imports. I've read up on it and I can see how to apply it. One problem I'd have with using it is that I've built up a fairly large codebase, and I haven't been too neat about it so most modules have a quick 'self-test' under if __name__ == '__main__':, which from what I've read does not play with relative imports:

编辑 - 感谢指向相对导入的指针。我已经阅读了它,我可以看到如何应用它。我使用它时遇到的一个问题是我已经构建了一个相当大的代码库,而且我对它没有太多的了解,所以大多数模块在if __name__ =='__ main__'下都有一个快速的“自我测试” :,从我读过的内容中,我没有使用相对导入:

The other solution I've been able to google up is to deliberately manipulate sys.path, which seems like an even worse hack. Are there any other possibilities?

我能够谷歌的其他解决方案是故意操纵sys.path,这似乎是一个更糟糕的黑客。还有其他可能性吗?

edit - Thanks for the suggestions. I'd originally misunderstood git branches, so as pointed out branches are exactly what I want. Nonetheless, I hadn't heard of relative imports before so thanks for that as well. I've learnt something new and may incorporate its use.

编辑 - 感谢您的建议。我原本误解了git分支,所以指出分支正是我想要的。尽管如此,我之前没有听说过相对进口,所以也要感谢。我已经学到了一些新东西并且可以将其用于其中。

3 个解决方案

#1


1  

Maybe I'm not understanding correctly, but it seems that git would be the solution here, since git's branches don't need separate paths.

也许我没有正确理解,但似乎git会成为解决方案,因为git的分支不需要单独的路径。

Create a branch for each working version of your eggs module. Then when you checkout that branch, the entire module is changed to a state matching the version of your sub-module. You could then merge what you need back and forth between the branches.

为egg模块的每个工作版本创建一个分支。然后,当您签出该分支时,整个模块将更改为与子模块版本匹配的状态。然后,您可以在分支之间来回合并所需的内容。

And as S.Lott pointed out, may a little refactoring couldn't hurt either ;)

正如S.Lott指出的那样,可能有一点重构也不会伤害;)

#2


3  

Relative imports (PEP 328) might help:

相对进口(PEP 328)可能会有所帮助:

eggs/
  __init__.py
  foo.py
  bar.py

# foo.py
from __future__ import absolute_import
from . import bar

See How do you organize Python modules? for other options.

请参阅如何组织Python模块?其他选择。

EDIT:

Yet another option is to use S.Lott's and Jim's suggestions i.e, restructure your package to factor out a eggs.foo part used by eggs.bar.a and use git to work on experimental branches (see Git Community Book).

另一个选择是使用S.Lott和Jim的建议,即重构你的包以分解egg.bar.a使用的eggs.foo部分并使用git在实验分支上工作(参见Git社区书)。

Here's an example:

这是一个例子:

$ git status
# On branch master
nothing to commit (working directory clean)

[just to make sure that all is good]

[只是为了确保一切都很好]

$ git checkout -b experimental
Switched to a new branch "experimental"

[work on experimental stuff]

[研究实验的东西]

$ git commit -a

[commit to experimental branch]

[承诺实验分支]

$ git checkout master
Switched to branch "master"

[work on master branch]

[在主分公司工作]

$ git commit -a

To merge changes into master branch:

要将更改合并到主分支:

$ git merge experimental

See chapter Basic Branching and Merging from the above book.

请参阅上一本书中的“基本分支和合并”一章。

#3


1  

"say I have a module 'eggs', with sub-modules 'foo' and 'bar'. Components in 'bar' use code in foo, so eggs/bar/a.py may 'import eggs.foo'."

“说我有一个模块'鸡蛋',子模块'foo'和'bar'。'bar'中的组件使用foo中的代码,所以egg / bar / a.py可能'导入eggs.foo'。”

This may not be the best structure. I suggest you have some other modules struggling to get out.

这可能不是最好的结构。我建议你有其他一些模块在努力摆脱困境。

You have eggs.bar.a depending on eggs.foo. I'm guessing other stuff on eggs depends on eggs.foo. Further, I suspect that eggs.foo could be partitioned into eggs.foo and eggs.quux and things might be simpler.

你有egg.bar.a取决于eggs.foo。我猜测鸡蛋上的其他东西取决于eggs.foo。此外,我怀疑eggs.foo可以分为egg.foo和eggs.quux,事情可能更简单。

I'd recommend refactoring this to get a better structure. The PYTHONPATH issues are symptomatic of too many things in the wrong places in the module tree.

我建议重构这个以获得更好的结构。 PYTHONPATH问题是模块树中错误位置的太多事情的症状。

#1


1  

Maybe I'm not understanding correctly, but it seems that git would be the solution here, since git's branches don't need separate paths.

也许我没有正确理解,但似乎git会成为解决方案,因为git的分支不需要单独的路径。

Create a branch for each working version of your eggs module. Then when you checkout that branch, the entire module is changed to a state matching the version of your sub-module. You could then merge what you need back and forth between the branches.

为egg模块的每个工作版本创建一个分支。然后,当您签出该分支时,整个模块将更改为与子模块版本匹配的状态。然后,您可以在分支之间来回合并所需的内容。

And as S.Lott pointed out, may a little refactoring couldn't hurt either ;)

正如S.Lott指出的那样,可能有一点重构也不会伤害;)

#2


3  

Relative imports (PEP 328) might help:

相对进口(PEP 328)可能会有所帮助:

eggs/
  __init__.py
  foo.py
  bar.py

# foo.py
from __future__ import absolute_import
from . import bar

See How do you organize Python modules? for other options.

请参阅如何组织Python模块?其他选择。

EDIT:

Yet another option is to use S.Lott's and Jim's suggestions i.e, restructure your package to factor out a eggs.foo part used by eggs.bar.a and use git to work on experimental branches (see Git Community Book).

另一个选择是使用S.Lott和Jim的建议,即重构你的包以分解egg.bar.a使用的eggs.foo部分并使用git在实验分支上工作(参见Git社区书)。

Here's an example:

这是一个例子:

$ git status
# On branch master
nothing to commit (working directory clean)

[just to make sure that all is good]

[只是为了确保一切都很好]

$ git checkout -b experimental
Switched to a new branch "experimental"

[work on experimental stuff]

[研究实验的东西]

$ git commit -a

[commit to experimental branch]

[承诺实验分支]

$ git checkout master
Switched to branch "master"

[work on master branch]

[在主分公司工作]

$ git commit -a

To merge changes into master branch:

要将更改合并到主分支:

$ git merge experimental

See chapter Basic Branching and Merging from the above book.

请参阅上一本书中的“基本分支和合并”一章。

#3


1  

"say I have a module 'eggs', with sub-modules 'foo' and 'bar'. Components in 'bar' use code in foo, so eggs/bar/a.py may 'import eggs.foo'."

“说我有一个模块'鸡蛋',子模块'foo'和'bar'。'bar'中的组件使用foo中的代码,所以egg / bar / a.py可能'导入eggs.foo'。”

This may not be the best structure. I suggest you have some other modules struggling to get out.

这可能不是最好的结构。我建议你有其他一些模块在努力摆脱困境。

You have eggs.bar.a depending on eggs.foo. I'm guessing other stuff on eggs depends on eggs.foo. Further, I suspect that eggs.foo could be partitioned into eggs.foo and eggs.quux and things might be simpler.

你有egg.bar.a取决于eggs.foo。我猜测鸡蛋上的其他东西取决于eggs.foo。此外,我怀疑eggs.foo可以分为egg.foo和eggs.quux,事情可能更简单。

I'd recommend refactoring this to get a better structure. The PYTHONPATH issues are symptomatic of too many things in the wrong places in the module tree.

我建议重构这个以获得更好的结构。 PYTHONPATH问题是模块树中错误位置的太多事情的症状。