简体   繁体   English

创建具有多个级别的文件夹的python包

[英]creating python package with multiple level of folders

I would like to create a package of my python code. 我想创建一个python代码包。 My folder structure is like below: 我的文件夹结构如下:

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)* 

i want to access class within linear.py by something like: 我想通过以下方式访问linear.py中的类:

from sing.ml.regression import linear

Please advice how to create a package like this 请建议如何创建这样的包

thanks in advance 提前致谢

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

        -classification
           __init__.py
           logistic.py

And if the working directory of application is not parent folder of sing then you need to register folder 'sing' into PYTHONPATH environment variable. 并且,如果应用程序的工作目录不是sing的父文件夹,那么您需要将文件夹sing注册到PYTHONPATH环境变量中。

For importing linear from sing folder you can use relative path: 要从sing文件夹导入线性 ,可以使用相对路径:

from ml.regression import linear

and for calling function of linear file you can use: 对于线性文件的调用功能,您可以使用:

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

You need to add a __init__.py files in every folder, so that python interpret the other *.py files (and folder) as packages. 您需要在每个文件夹中添加__init__.py文件,以便python将其他* .py文件(和文件夹)解释为包。 A empty file is enough. 一个空文件就足够了。

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

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