简体   繁体   English

Python:如何从另一个目录打开文件 git repo(本地克隆)?

[英]Python: how to open files from another directory git repo (locally cloned)?

I'm currently working on a Python module in a git repo and need to read files from another directory in the same repo.我目前正在处理 git 存储库中的 Python 模块,并且需要从同一存储库中的另一个目录读取文件。 I have the repo cloned locally, so my module can currently open local files saved in the same directory as my module, but I need to be able to open other files from other directories within the same repo.我在本地克隆了 repo,因此我的模块当前可以打开与我的模块保存在同一目录中的本地文件,但我需要能够从同一 repo 中的其他目录打开其他文件。 What's the best way I can do this?我能做到这一点的最好方法是什么? Thanks so much for any help!非常感谢您的帮助!

Here's how we do it where I work!这就是我们在我工作的地方的做法!

Lets say the directory looks like so:假设目录如下所示:

User -|
      |-GitHub/-
               |- dir_1/
               |
               |- dir_2/ -
                        |- module_path_1/ -
                                        | - module.py

We are in dir_1, running our code from dir_1.我们在 dir_1,从 dir_1 运行我们的代码。

import sys
sys.path.append("../dir_2/module_path_1")
from module import function1

And that should do it!那应该这样做!

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

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