[英]Cannot import from a git submodule
I'm having difficulties making a git submodule work.我在使 git 子模块工作时遇到困难。
I have a project ProjectA
that basically is a mainA.py
file and a subfolder with library files: The mainA.py
contains a MainClass
that is basically what should be called, and Libraries
just contain scripts and classes for computations.我有一个项目
ProjectA
,它基本上是一个mainA.py
文件和一个包含库文件的子文件夹: mainA.py
包含一个MainClass
,它基本上应该被调用,而Libraries
只包含用于计算的脚本和类。
ProjectA/
Libraries/
__init__.py
library1.py
library2.py
__init__.py
mainA.py
In mainA.py
I just do something like:在
mainA.py
我只是做这样的事情:
# content of mainA.py
from Libraries.library1 import ClassA, ClassB
class MainClass:
# do stuff
if __name__ == '__main__':
MainClass()
This just works fine, but I have now a ProjectB
that needs to use the MainClass
from ProjectA
, so I decided to put ProjectA
as a git submodule of ProjectB
这很好用,但我现在有一个
ProjectB
需要使用来自ProjectA
的MainClass
,所以我决定将ProjectA
作为ProjectB
的 git 子模块
git submodule add ProjectA_git_url
ProjectB/
ProjectA/
mainB.py
.gitmodules
However now in mainB.py
I'm trying to import MainClass
from projectA
.但是现在
mainB.py
我试图导入MainClass
从projectA
。
# content of mainB.py
from ProjectA.mainA import MainClass
ModuleNotFoundError: No module named 'Libraries'
I think this happens because now Libraries
is no longer hanging from the root directory, but inside the submodule ProjectA
, so when mainA.py
does:我认为这是因为现在
Libraries
不再挂在根目录中,而是挂在子模块ProjectA
,所以当mainA.py
这样做时:
from Libraries.library1 import ClassA, ClassB
The system cannot find Libraries
.系统找不到
Libraries
。 If I change mainA.py
to do:如果我将
mainA.py
更改为:
from ProjectA.Libraries.library1 import ClassA, ClassB
Then it works, but of course I don't want to change anything insise ProjectA
, it is just a Project that should work either standalone or as a submodule of another project然后它就可以工作了,但当然我不想改变任何内部的
ProjectA
,它只是一个应该独立工作或作为另一个项目的子模块工作的项目
What am I doing wrong?我究竟做错了什么? Is there a way to import
MainClass
from mainA.py
when ProjectA
is a submodule?当
ProjectA
是子模块时,有没有办法从mainA.py
导入MainClass
?
git
is a development tool; git
是一个开发工具; you use it during development but not deployment.您在开发期间使用它,而不是在部署期间使用它。
pip
is a deployment tool; pip
是一个部署工具; during development you use it to install necessary libraries;在开发过程中,您使用它来安装必要的库; during deployment your users use it to install your package with dependencies.
在部署期间,您的用户使用它来安装具有依赖项的包。
Use submodules when you need something from a remote repository in your development environment.当您需要开发环境中的远程存储库中的某些内容时,请使用子模块。 For example, if said remote repository contains Makefile(s) or other non-python files that you need and that usually aren't installed with
pip
.例如,如果所述远程存储库包含您需要的 Makefile(s) 或其他非 python 文件,并且通常不会与
pip
安装。
That is, in your case you shouldn't make ProjectA
a submodule of ProjectB
, you should make ProjectA
a Python dependency.也就是说,在您的情况下,您不应将
ProjectA
ProjectB
的子模块,而应将ProjectA
Python 依赖项。 Create packages for ProjectA
and ProjectB
and install them separately but allow ProjectB
to import from ProjectA
.为
ProjectA
和ProjectB
创建包并单独安装它们,但允许ProjectB
从ProjectA
导入。
Dependencies are declared in setup.py
or requirements.txt
.依赖项在
setup.py
或requirements.txt
中声明。
That said, if you insist on using submodules: either you have to manipulate sys.path
yourself or you do relative import in mainA.py
:也就是说,如果您坚持使用子模块:要么您必须自己操作
sys.path
要么您在mainA.py
进行相对导入:
from .Libraries.library1 import ClassA, ClassB
将子模块路径添加到 mainB.py 中的系统路径假设您的子模块路径是“../ProjectB/ProjectA” sys.path.append(../ProjectB/ProjectA) 解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.