具有自己的软件包并与git共享的Python3框架:如何处理sys.path和__init__.py文件?

时间:2018-07-27 10:18:09

标签: python python-3.x git packages sys.path

我开始用Python 3构建数据处理框架。目标是开发自己的程序包并编写示例代码以使用这些程序包。该框架将由git与其他研究人员共享(可能会开发)。

在多台计算机上处​​理搜索路径的最佳方法是什么? __ init __。py 文件在目录中应如何显示?

我正在考虑使用 sys.path.append(os.getcwd(相对于主目录Python: Best way to add to sys.path relative to the current running script)来添加 sys.path )) sys.path.append(“ ..”)我不知道这是在具有不同绝对路径和操作系统的不同机器上从顶级导入的好方法。 (超出顶级软件包的错误/问题:beyond top level package error in relative import

基本目录结构可能是(稍后我将添加更多软件包和处理功能):

main_directory/
main_directory/__init__.py
main_directory/packages/
main_directory/packages/__init__.py
main_directory/packages/preprocessing/
main_directory/packages/preprocessing/filters.py    #implementation of multiple classes: e.g. fillter1 and filter2
main_directory/packages/preprocessing/__init__.py
main_directory/packages/postprocessing/
main_directory/packages/postprocessing/filters.py   #implementation of multiple classes: e.g. fillterA and filterB
main_directory/packages/postprocessing/__init__.py
main_directory/examples/
main_directory/examples/easy_example.py #file with examples to use the filters
main_directory/your_code/
main_directory/your_code/your_code.py   #file with your code to use the filters

1 个答案:

答案 0 :(得分:1)

有一个标准的包装布局。如果您遵循该步骤,则无需触摸sys.path就可以部署程序。

使用此网站上的资源并学习设置工具。

https://packaging.python.org/tutorials/packaging-projects/

更新

在开发过程中,您可以使用虚拟环境。 首先创建一个:

$ virtualenv venv

使用-e安装软件包会创建指向目录的链接。

$ venv/bin/pip install -e yourproject.git

当您在虚拟环境中启动python时,导入应该起作用。

$ venv/bin/python
>>> import preprocessing.filters
>>>