簡體   English   中英

創建具有多個級別的文件夾的python包

[英]creating python package with multiple level of folders

我想創建一個python代碼包。 我的文件夾結構如下:

sing*(this is a folder)*
--ml*(this is a folder)*
----regression*(this is a folder)*
------linear.py*(this is a class file with functions in it)* 
----classification*(this is a folder)*
------logistic.py*(this is a class file with functions in it)* 

我想通過以下方式訪問linear.py中的類:

from sing.ml.regression import linear

請建議如何創建這樣的包

提前致謝

sing
    __init__.py
    -ml
        __init__.py
        -regression
           __init__.py
           linear.py

        -classification
           __init__.py
           logistic.py

並且,如果應用程序的工作目錄不是sing的父文件夾,那么您需要將文件夾sing注冊到PYTHONPATH環境變量中。

要從sing文件夾導入線性 ,可以使用相對路徑:

from ml.regression import linear

對於線性文件的調用功能,您可以使用:

linear.<*functionname*>(...)

您需要在每個文件夾中添加__init__.py文件,以便python將其他* .py文件(和文件夾)解釋為包。 一個空文件就足夠了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM