简体   繁体   中英

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?

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.

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