在项目中有这么多的目录,我希望每个.py
文件在单独的目录中,正确地导入其他文件。你知道吗
我试过了
PREFIX = normpath(dirname(dirname(dirname(abspath(__file__)))))
if PREFIX not in sys.path:
sys.path = [PREFIX] + sys.path
当我想导入其他目录variables
时,我只导入这个文件。但是可能太冗长了。你知道吗
有没有更好的方法以更好的方式解决它(除了将项目路径导出到全局系统路径的方法)?你知道吗
Tags:
您可以使用python模块并像这样导入它们:
例如:
文件夹包括:
比如:https://github.com/masom/Puck/blob/master/client/pixie/controllers/configuration.py#L2
^{} statement 包括对
packages
文件(模块)的支持,这些文件(模块)排列在带有__init__.py
标记的目录中。你知道吗提供了对相对包层次结构(使用前导点)的支持, 因此,绝对模块参考是没有必要的。你知道吗
您可以将项目安排为子包,并使用类似于的相对导入
from ..subpkg2 import mod
使用subpkg2
中mod
的变量 (来自同一级别的任何其他子包)。你知道吗相关问题 更多 >
编程相关推荐