简体   繁体   English

从父级导入的 Python 相对导入,没有 sys.path

[英]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.我强烈建议您将 git 存储库分成两部分 - 一个用于框架的存储库,一个用于使用它的项目。 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 .然后正确打包框架以便 pip 可以安装它(无需将其发送到 pipy, pip 可以从 git 安装),为“软件”项目创建一个 virtualenv ,并使用“可编辑”标志在 virtualenv 中 pip 安装框架.

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.我知道这可能看起来像很多工作,但实际上它比你可能担心的更容易和更快,而且根据经验(我的意思是在很多项目上有多年的经验),它是导致更少头痛和问题的解决方案从长远来看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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