How can I work with multiple git branches of a python module?

I want to use git to allow me to work with several functions in a module that I am writing at the same time. I am currently using SVN with only one workspace, so I just have a workspace on PYTHONPATH. I understand that this is less than ideal, so I was wondering if anyone could suggest a more “right” way to do this.

Let me clarify a hypothetical situation: I say that I have an egg module, with submodules foo and bar. The components in the bar use the code in foo, so egg / bar / a.py can import eggs.foo.

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

How to configure yourself so that each copy of the module uses its own "foo"? Thanks.

edit- Thanks for the pointer to relative imports. I read about it and I see how to apply it. One of the problems that I would encounter is that I created a rather large code base and I was not too neat, so most modules have a quick "self-test" under if __name__ == '__main__': from which I read that it does not play with relative imports:

Another solution I was able to execute google with is to intentionally manipulate sys.path, which seems to be an even worse hack. Are there any other options?

edit - Thanks for the suggestions. I initially misunderstood the git branches, since the indicated branches are exactly what I want. However, I have not heard of relative imports before, thanks for that. I learned something new and can turn on its use.

+2
source share
3 answers

Maybe I don’t understand correctly, but it seems that there will be git, since git branches do not need separate paths.

Create a branch for each working version of your egg module. Then, when you check this thread, the entire module changes to the state corresponding to the version of your submodule. Then you can combine everything you need between the branches.

And as S. Lott noted, can a little refactoring not hurt;)

+1
source

Relative imports ( PEP 328 ) can help:

 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 parameters.

EDIT:

Another option is to use the sentences S.Lott and Jim, i.e. rebuild your package to separate the eggs.foo part used by eggs.bar.a and use git to work with experimental branches (see the Git Community Book ).

Here is an example:

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

[just to make sure everything is fine]

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

[work on experimental materials]

 $ git commit -a 

[pass to the experimental branch]

 $ git checkout master Switched to branch "master" 

[work on the main branch]

 $ git commit -a 

To merge changes to the main branch:

 $ git merge experimental 

See the chapter Main Branching and Merging from the above book.

+3
source

"Let's say I have an egg module, with submodules foo and bar. The components in the bar use the code in foo, so egg / bar / a.py can import eggs.foo,."

This is not a good structure. I offer you some more modules that are trying to exit.

You have eggs.bar.a depending on eggs.foo . I guess the other things on eggs depend on eggs.foo . In addition, I suspect that eggs.foo can be divided into eggs.foo and eggs.quux , and everything could be simpler.

I would recommend refactoring to get a better structure. PYTHONPATH problems are symptoms of too many things in the wrong places in the module tree.

+1
source

All Articles