简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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