2

I have this structure:

Git
 |
 |-->Framework
 |          |
 |          '---> framework.py
 |
 '-->Software hanging on framewok
            |
            '---> main.py // here i need inherit class from framework.py module.

So far, I use the following procedure which I found in many other replies in this forum:

import sys
sys.path.append("../Framework")
from framework import Framework

class Main(Framework):
    def __init__(self):
        super().__init__()

But I really don't like to add paths everytime I need something from framework package. Is there some workaround?

colidyre
  • 4,170
  • 12
  • 37
  • 53
Erik Šťastný
  • 1,487
  • 1
  • 15
  • 41
  • 2
    May I ask where exactly you found this `sys.path` hackery so I can downvote it? – Aran-Fey Sep 20 '18 at 10:12
  • @Aran-Fey For example here https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder with 178 upvotes. – Erik Šťastný Sep 20 '18 at 10:13
  • 1
    Thanks. I guess someone will have to post a proper answer on that question... so much bad (or incomplete) advice... – Aran-Fey Sep 20 '18 at 10:15
  • By the way, is your `Git` folder a package? The solution for your problem will be different depending on whether `Framework` and `Software` are independent packages, or if they're subpackages of the larger `Git` package. – Aran-Fey Sep 20 '18 at 10:19
  • Nope, Git is root of repository so they are independent. – Erik Šťastný Sep 20 '18 at 10:25
  • is `Framework` installable with pip/setup.py? The initial project structure is quite bad, you need `Framework` in some discoverable location, not isolated directory. – Evgeny Sep 20 '18 at 10:52
  • @EPo No, It is not. There is much more "Software" folders which are hanging on framework. So i can not put Framework folder inside of Software folder. – Erik Šťastný Sep 20 '18 at 14:44
  • Looks like you you might want to make framework locally installable, as @bruno desthuilliers suggests in his answer. Tweaking `sys.path` is a rather dirty way to bundle a python project, if other options are available. – Evgeny Sep 21 '18 at 07:35

1 Answers1

1

I strongly suggest that you split your git repository in two - one repo for the framework, one for the project using it. Then properly package the framework so pip can install it (no need to send it to pipy, pip can install from git), create a virtualenv for the "software" project, and pip install the framework in the virtualenv with the "editable" flag.

I understand that it might seem like a lot of work but it's actually much easier and faster to do than you might fear, and from experience (I mean years of experience on lot of projects) it's the solution that will cause the less headaches and issues in the long run.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118