且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从git子模块导入python文件

更新时间:2023-08-21 12:30:04

您可以将其添加到要访问该模块的文件的sys.path中,例如:

you can add to sys.path in the file you want to be able to access the module, something like:

import sys
sys.path.append("/home/me/myproj/gitmodules")
import mygitsubmodule

此示例将路径添加为原始字符串,以使其清楚正在发生的事情.您确实应该使用下面描述的更复杂,与系统无关的方法来确定和组装路径.

This example is adding a path as a raw string to make it clear what's happening. You should really use the more sophisticated, system independent methods described below to determine and assemble the path.

此外,当我使用此方法时,我发现使用sys.path.insert(1, ..更好,因为某些功能似乎依赖于sys.path[0]作为程序的起始目录.

Also, I have found it better, when I used this method, to use sys.path.insert(1, .. as some functionality seems to rely of sys.path[0] being the starting directory of the program.