Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建具有多级文件夹的python包_Python - Fatal编程技术网

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

创建具有多级文件夹的python包,python,Python,我想创建一个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

我想创建一个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
请建议如何创建这样的包


提前感谢

您需要在每个文件夹中添加一个
\uuuu init\uuuu.py
文件,以便python将其他*.py文件(和文件夹)解释为包。一个空文件就足够了

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*>(...)
线性。(…)

您应该已经能够从使用绝对导入的位置访问linear,我相信我的消费者应用程序托管在azure上。@Shinratensei,您是说如果我在“sing”文件夹中有一个test.py文件,那么在test.py中,我可以通过说“sing.ml.regression import linear”来调用linear.py类吗?从我在其他问题中读到的内容来看,是的,您应该能够得到以下错误“ValueError:在非包中尝试相对导入”错误是“TypeError:“module”对象不可调用”,代码是import os.chdir(“/Users/Documents/sing”);从ml.regression导入Linear obj=Linear(),即使我尝试“Linear.(…)并得到相同的错误