1

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). To solve this I add on top of run.py of A the following:

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? Is this the only way of making a GIT submodule work within a python project?

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

UPDATE 2: There is indeed a way of correcting ERROR 1 without having to put :

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). More info here 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. B calls its own data class. However when I call B from A, B call data class from A and not its own...how can I fix this?

alect
  • 103
  • 1
  • 6
  • 1
    This has nothing to do with git submodules, Python is not aware of anything related to git. This is merely due to the directory structure, regardless of some dirs being git submodules – DeepSpace Feb 03 '22 at 11:24
  • By GIT submodule I meant the project B has the structure of a project (src folder, readme , config files etc...) and not just one PYTHON submodule. – alect Feb 03 '22 at 12:22

1 Answers1

0

It s been a long day...


To fix ERROR2 I changed the structure of project B as following simplified directory tree shows.

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
  2. got rid of virtualenv of A
  3. set it again
  4. installed B as a package

Voila it worked!

Now, when calling B from A then B calls its own Data class. I reworked the imports in B to reflect the changes of the project I made. Then Pylance complained (for a change) so I just quick-fixed it.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
alect
  • 103
  • 1
  • 6