Erik Šťastný
Erik Šťastný

Reputation: 1486

Python relative import from parent without sys.path

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?

Upvotes: 2

Views: 209

Answers (1)

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

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.

Upvotes: 1

Related Questions