我有这样的文件夹结构:

application
├── app
│   └── folder
│       └── file.py
└── app2
    └── some_folder
        └── some_file.py

如何从file.py或some_file.py中导入函数?我尝试了:

from application.app.folder.file import func_name

但它不起作用。


当前回答

创建包的最佳实践是从最高级别目录中的main_module.py等模块运行和访问其他模块。

此结构说明您可以使用顶级目录文件main_module.py来使用和访问子包、父包或同级包和模块。

创建并运行这些文件和文件夹以进行测试:

 package/
    |
    |----- __init__.py (Empty file)
    |------- main_module.py (Contains: import subpackage_1.module_1)        
    |------- module_0.py (Contains: print('module_0 at parent directory, is imported'))
    |           
    |
    |------- subpackage_1/
    |           |
    |           |----- __init__.py (Empty file)
    |           |----- module_1.py (Contains: print('importing other modules from module_1...')
    |           |                             import module_0
    |           |                             import subpackage_2.module_2
    |           |                             import subpackage_1.sub_subpackage_3.module_3)
    |           |----- photo.png
    |           |
    |           |
    |           |----- sub_subpackage_3/
    |                        |
    |                        |----- __init__.py (Empty file)
    |                        |----- module_3.py (Contains: print('module_3 at sub directory, is imported')) 
    |
    |------- subpackage_2/
    |           |
    |           |----- __init__.py (Empty file)
    |           |----- module_2.py (Contains: print('module_2 at same level directory, is imported'))

现在运行main_module.py

输出为

>>>'importing other modules from module_1...'
   'module_0 at parent directory, is imported'
   'module_2 at same level directory, is imported'
   'module_3 at sub directory, is imported'

打开图片和文件注意事项:

在包结构中,如果您想访问照片,请使用最高级别目录中的绝对目录。

假设您正在运行main_module.py,并且希望在module_1.py中打开photo.png。

module_1.py必须包含的内容是:

对的:

image_path = 'subpackage_1/photo.png'
cv2.imread(image_path)

错误:

image_path = 'photo.png'
cv2.imread(image_path)

尽管module1.py和photo.png位于同一目录。

其他回答

以防有人仍在寻找解决方案。这对我有用。

Python将包含您启动的脚本的文件夹添加到PYTHONPATH中,因此如果您运行

python application/app2/some_folder/some_file.py

只有文件夹application/app2/some_folder被添加到路径(而不是执行命令的基本目录)。相反,将文件作为模块运行,并在some_folder目录中添加__init__.py。

python -m application.app2.some_folder.some_file

这将把基本目录添加到python路径,然后可以通过非相对导入访问类。

在linux上的python3中为我工作

import sys  
sys.path.append(pathToFolderContainingScripts)  
from scriptName import functionName #scriptName without .py extension  

我认为一种特别的方法是使用环境变量PYTHONPATH,如文档中所述:Python2,Python3

# Linux & OSX
export PYTHONPATH=$HOME/dirWithScripts/:$PYTHONPATH

# Windows
set PYTHONPATH=C:\path\to\dirWithScripts\;%PYTHONPATH%

我也面临着同样的挑战,尤其是在导入多个文件时,这就是我如何克服的。

import os, sys

from os.path import dirname, join, abspath
sys.path.insert(0, abspath(join(dirname(__file__), '..')))

from root_folder import file_name

哇,我没想到会花这么多时间在这上面。以下内容对我有用:

操作系统:Windows 10

Python:v3.10.0

注意:由于我是Python v3.10.0,所以我没有使用__init__.py文件,这对我来说无论如何都不起作用。

application
├── app
│   └── folder
│       └── file.py
└── app2
    └── some_folder
        └── some_file.py

王旭的第一个解决方案对我有效。为了清楚起见,我将其转发了一个绝对的文件参考:

import sys
sys.path.insert(1, 'C:\\Users\\<Your Username>\\application')
import app2.some_folder.some_file

some_file.hello_world()

替代解决方案:然而,这也对我有效:

import sys
sys.path.append( '.' )
import app2.some_folder.some_file

some_file.hello_world()

虽然,我不明白为什么它会起作用。我以为点是对当前目录的引用。但是,当打印出当前文件夹的路径时,当前目录已列在顶部:

for path in sys.path:
    print(path)

希望有人能在评论中澄清为什么这样做有效。尽管如此,我也希望它能帮助一些人。