简体   繁体   English

为 github 存储库配置本地 python package 导入的最佳实践?

[英]Best practice for configuring local python package imports for github repo?

I'm newish to imports so it's possible my approach is patchy but i'm really struggling to piece together how this is supposed to be done.我对进口很陌生,所以我的方法可能不完整,但我真的很难拼凑出应该如何完成的工作。

So I have a github repo I am working on that currently looks something like this:所以我有一个 github repo 我正在处理,目前看起来像这样:

Github-repo-name
   /src
      /repo-name
         __init__.py
         /packages
            __init__.py
            mypackage1/
               __init__.py
               p1_module1.py
            mypackage2/
               __init__.py
               p2_module1.py
         main.py
      /scripts
         do_something.py
      /datasets

I'm trying to import mypackage1 into do_something.py in order to generate some data to put in datasets/ but was having lots of trouble when I ran the do_something.py script in the src directory.我正在尝试将 mypackage1 导入 do_something.py 以生成一些数据以放入 datasets/ 中,但是当我在src目录中运行 do_something.py 脚本时遇到了很多麻烦。

I had this line in do_something.py:我在 do_something.py 中有这一行:

import repo-name.packages.mypackage1

and was hitting: ModuleNotFoundError: No module named repo-name并且被击中: ModuleNotFoundError:没有名为 repo-name 的模块

I tried relative imports to no more success我尝试了相对进口,但没有成功

from ..repo-name.packages import mypackage1

ImportError: attempted relative import with no known parent package ImportError:尝试在没有已知父 package 的情况下进行相对导入

I couldn't make head or tail of this issue so I instead tried adding my packages to the path in do_something.py我无法解决这个问题,所以我尝试将我的包添加到 do_something.py 的路径中

file_path = pathlib.Path(__file__)
path = file_path.resolve().parents[2]
sys.path.append(str(path))

import repo-name.packages.mypackage1

Success!成功!

Except now i'm wondering if this really what I should be doing and if this isn't a bit hacky.除了现在我想知道这是否真的是我应该做的,如果这不是有点 hacky。 The idea of needing to add the path at runtime seems a bit messy so i'd rather have it all the time.需要在运行时添加路径的想法似乎有点混乱,所以我宁愿一直拥有它。 I've read I can use PYTHONPATH for this purpose which would be great if it were just me using this repo, but I intend for this to be shared and reproduceable.我已经读过我可以使用 PYTHONPATH 来达到这个目的,如果只有我使用这个 repo 那就太好了,但我打算共享和复制它。 I thought about maybe writing a shell script to set the PYTHONPATH but i'm not sure that's much better.我想也许写一个 shell 脚本来设置 PYTHONPATH 但我不确定这会好得多。

So my question is what is the right/better/a good way of doing what i'm trying to do?所以我的问题是做我想做的事情的正确/更好/好方法是什么?

Oliver.奥利弗。
I don't know what is the best way, but I'm trying to show you how our team works.我不知道什么是最好的方法,但我想向您展示我们团队的工作方式。

When developing,开发时,
i) register PYTHONPATH in ~/.bashrc i) 在~/.bashrc中注册PYTHONPATH
ii) sys.path.append(path/to/root/repo) on the top of the code ii) sys.path.append(path/to/root/repo)在代码的顶部

Our team usually does it as an initial way.我们的团队通常将其作为初始方式。

To distinguish whether they receive the authority of repo or not, you can check the below.要区分他们是否获得repo的权限,您可以检查以下内容。

# Suppose you created a setup.py(to make `.whl` file) on `repo-name`.
if __name__ == '__main__':
    from repo-name.packages.mypackage1 import *
else:
    from ..repo-name.packages.mypackage1 import *

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

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