简体   繁体   English

Git子模块在python项目中使用后引发导入错误

[英]Git submodule raises import error once used in python project

I'm using a git-submodule in my python project. 我在python项目中使用了git-submodule。

The submodule project looks like this: 子模块项目如下所示:

-submodule_project
    - __init__.py
    - debug_util.py
    - parsing_util
        - __init__.py
        - parse.py
        - consts.py

parse.py imports debug_util.py . parse.py导入debug_util.py This structure works fine as the submodule is an independent project. 由于子模块是一个独立的项目,因此此结构可以正常工作。

My project is built like this: 我的项目是这样构建的:

-project
    - __init__.py
    - file1.py
    - some_dir
        - __init__.py
        - main.py

So once I use the submodule as a git submodule in my project, the parse.py raises ImportError . 因此,一旦我将子模块用作项目中的git子模块, parse.py引发ImportError This happens once the line which imports the debug_util.py is ran. 一旦运行了导入debug_util.py的行,就会发生这种情况。 Just to clarify: main.py imports parse.py , which imports debug_util.py 只是澄清main.pymain.py导入parse.py ,后者导入debug_util.py

Can you explain me what i'm doing wrong, and what are the available solutions to fix this? 您能解释一下我做错了什么吗?有哪些可用的解决方案可以解决此问题?

Here's my .gitmodules file: 这是我的.gitmodules文件:

[submodule "submodule_project"]
path = submodule_project
url = ../submodule_project.git

Thanks in advance for all of you! 预先感谢大家!

Git Submodules are very annoying to work with (at least they were last time I played around with them). Git子模块非常烦人(至少是我上次玩它们的时候)。 I'd recommend against using submodules and simply using python's own dependency management. 我建议不要使用子模块,而应该仅使用python自己的依赖项管理。 So your submodule_project would have its own unique name and get packaged up in releases like myparser-1.2.1 , and then your main project would depend on that package from its setup.py . 因此,您的submodule_project将具有其自己的唯一名称,并打包在myparser-1.2.1类的发行版中,然后您的主项目将依赖于setup.py那个软件包。

Problems with git submodules (from the git documentation): git子模块的问题(来自git文档):

  • When you clone [a project with submodules], by default you get the directories that contain submodules, but none of the files within them yet 克隆[带有子模块的项目]时,默认情况下会获得包含子模块的目录,但其中没有文件
  • You must run two commands: git submodule init to initialize your local configuration file, and git submodule update to fetch all the data from that project and check out the appropriate commit listed in your superproject 您必须运行两个命令: git submodule init初始化您的本地配置文件,以及git submodule update以从该项目中获取所有数据并检出超级项目中列出的适当提交
  • If you create a new branch, add a submodule there, and then switch back to a branch without that submodule, you still have the submodule directory as an untracked directory 如果创建一个新分支,在其中添加一个子模块,然后切换回不包含该子模块的分支,则该子模块目录仍为未跟踪目录
  • Removing the directory isn't difficult, but it can be a bit confusing to have that in there. 删除目录并不困难,但是在目录中添加目录可能会有些混乱。 If you do remove it and then switch back to the branch that has that submodule, you will need to run submodule update --init to repopulate it. 如果确实要删除它,然后切换回具有该子模块的分支,则需要运行submodule update --init来重新填充它。
  • It's quite likely that if you're using submodules, you're doing so because you really want to work on the code in the submodule at the same time as you're working on the code in the main project (or across several submodules). 如果您正在使用子模块,那么这样做很有可能是因为您确实想在处理主项目中的代码的同时(或跨多个子模块)处理子模块中的代码。 。 Otherwise you would probably instead be using a simpler dependency management system (such as Maven or Rubygems). 否则,您可能会改用较简单的依赖项管理系统(例如Maven或Rubygems)。

Problems with git submodules (my own observations): git子模块的问题(我自己的观察):

  • You often find your submodules in weird states: 您经常会发现处于奇怪状态的子模块:
    • You wanted to peg a submodule at a specific git commit, but now it's drifted somehow and your top level project says there are changes involving your submodule. 您想将子模块固定在特定的git commit上,但是现在它已经以某种方式漂移了,并且您的顶级项目说有涉及子模块的更改。
    • Somehow file keep changing inside the submodule directory and git complains about unstaged changes in either the top level or submodule. 文件以某种方式在子模块目录中不断变化,而git抱怨顶级模块或子模块中未进行的临时更改。
    • You wanted a submodule to track master , but it's not working correctly and now you've got merge commits that aren't upstream. 您希望有一个子模块来跟踪master ,但是它不能正常工作,现在您已经获得了不在上游的合并提交。
  • It's annoying enough to update and init one level deep submodules, but what if one of your submodules also uses submodules? updateinit一级深层子模块已经很烦人了,但是如果您的子模块之一也使用子模块怎么办?
  • A lot of third-party tools don't work well with submodules. 许多第三方工具无法与子模块配合使用。 I've found that a lot of third-party tools (like some IDEs or web interfaces to git) don't really treat the core parts of git well (dealing with the staging area, merges, rebases, squashing, writing well formatted commit messages, etc), but they're especially bad at features even the most experienced git users rarely ever use. 我发现很多第三方工具(例如某些IDE或git的Web界面)并不能很好地对待git的核心部分(处理登台区域,合并,重新设置基数,压缩,编写格式正确的提交)消息等),但即使是经验最丰富的git用户很少使用的功能,它们也特别糟糕。

You also don't mention how and where you set up the submodule from the top level project. 您也没有提到在顶级项目中如何以及在何处设置子模块。 It might be more helpful if you pasted the .gitmodules file from the top level project. 如果您从顶级项目中粘贴.gitmodules文件,则可能会更有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM