简体   繁体   English

在 python 项目中为 GIT 子模块设置路径?

[英]Setting path for GIT submodule within a python project?

I have two projects A and B. B is a git submodule being called in a script called run.py of A. When I run it, fails telling me it can't find some inner module of B (ERROR 1).我有两个项目 A 和 B。B 是 git 子模块,在 A 的 run.py 脚本中被调用。当我运行它时,无法告诉我它找不到 B 的某些内部模块(错误 1)。 To solve this I add on top of run.py of A the following:为了解决这个问题,我在 A 的 run.py 之上添加了以下内容:

import sys
sys.path.append("pathToProjectB")

And it works.它有效。 However does anyone know another way of doing this?但是,有人知道另一种方法吗? Perhaps some better project structure (B has to be a GIT submodule of A, that can't be changed) or some configuration in the virtual environment folder?也许一些更好的项目结构(B 必须是 A 的 GIT 子模块,不能更改)或虚拟环境文件夹中的一些配置? Is this the only way of making a GIT submodule work within a python project?这是使 GIT 子模块在 python 项目中工作的唯一方法吗?

UPDATE 1: Moreover if I have some file in B that has the same name as a file in A then python can gets the file of A while I want it to get the file of B (ERROR 2).更新1:此外,如果我在B中有一些文件与A中的文件同名,那么python可以获取A的文件,而我希望它获取B的文件(错误2)。

UPDATE 2: There is indeed a way of correcting ERROR 1 without having to put:更新2:确实有一种纠正错误1的方法,而不必输入:

import sys
sys.path.append("pathToProjectB")

in top of run.py of project A. You do it by installing project B as a package in A and not just having it as a git submodule in your project structure of A: pip install -e./ProjectB but you have to create a setup.py file in Project B first (and a readme if the setup.py uses it).在项目 A 的 run.py 顶部。您可以通过在 A 中将项目 B 安装为 package 而不仅仅是将其作为 git 子模块安装在 A 的项目结构中:Z62AD1C2A46C5298F03E2C95D3BBA8首先是项目 B 中的 setup.py 文件(如果 setup.py 使用它,还有自述文件)。 More info here https://shunsvineyard.info/2019/12/23/using-git-submodule-and-develop-mode-to-manage-python-projects/更多信息在这里https://shunsvineyard.info/2019/12/23/using-git-submodule-and-develop-mode-to-manage-python-projects/

I am still having problems with ERROR2... so A and B have a some file called data with some class data.我仍然遇到 ERROR2 的问题...所以 A 和 B 有一个名为 data 的文件,其中包含一些 class 数据。 B calls its own data class. B调用自己的数据class。 However when I call B from A, B call data class from A and not its own...how can I fix this?但是,当我从 A 调用 B 时,B 从 A 调用数据 class 而不是它自己的......我该如何解决这个问题?

It s been a long day...这是漫长的一天...


To fix ERROR2 I changed the structure of project B as following simplified directory tree shows.为了修复 ERROR2,我更改了项目 B 的结构,如下所示的简化目录树所示。

From initial structure:从初始结构:

B 
    __init__.py
    setup.py
    run.py
    data
        __init__.py
        Data.py

Changed to:变成:

ProjectB
        run.py
        setup.py
        B
            __init__.py
            data
                 __init__
                 Data

Further steps进一步的步骤

  1. I updated B in A with git submodule update --remote B我用 git submodule update --remote B 更新了 A 中的 B
  2. got rid of virtualenv of A摆脱了 A 的 virtualenv
  3. set it again重新设置
  4. installed B as a package安装 B 作为 package

Voila it worked!瞧,它奏效了!

Now, when calling B from A then B calls its own Data class.现在,当从 A 调用 B 时,B 调用它自己的 Data class。 I reworked the imports in B to reflect the changes of the project I made.我重新设计了 B 中的导入以反映我所做的项目的更改。 Then Pylance complained (for a change) so I just quick-fixed it.然后 Pylance 抱怨(要求改变),所以我只是快速修复它。

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

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