我有这样的文件夹结构:

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

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

from application.app.folder.file import func_name

但它不起作用。


当前回答

您可以通过按f5刷新Python shell,或转到运行->运行模块。这样,您就不必更改目录来读取文件中的内容。Python将自动更改目录。但是,如果您想使用PythonShell中不同目录中的不同文件,那么可以在sys中更改目录,正如Cameron之前所说的那样。

其他回答

只需使用os模块中的change-dir函数:

os.chdir("Here new director")

比正常情况下可以导入的更多信息

将应用程序移动到其他环境时,使用带有绝对路径的sys.path.append并不理想。使用相对路径并不总是有效的,因为当前工作目录取决于脚本的调用方式。

由于应用程序文件夹结构是固定的,因此我们可以使用os.path获取要导入的模块的完整路径。例如,如果这是结构:

/home/me/application/app2/some_folder/vanilla.py
/home/me/application/app2/another_folder/mango.py

假设您要导入芒果模块。您可以在vanilla.py中执行以下操作:

import sys, os.path
mango_dir = (os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
+ '/another_folder/')
sys.path.append(mango_dir)
import mango

当然,您不需要mango_dir变量。

要了解其工作原理,请查看以下交互式会话示例:

>>> import os
>>> mydir = '/home/me/application/app2/some_folder'
>>> newdir = os.path.abspath(os.path.join(mydir, '..'))
>>> newdir
    '/home/me/application/app2'
>>> newdir = os.path.abspath(os.path.join(mydir, '..')) + '/another_folder'
>>> 
>>> newdir
'/home/me/application/app2/another_folder'
>>> 

并检查os.path文档。

还值得注意的是,使用包时,处理多个文件夹更容易,因为可以使用虚线模块名称。

创建包的最佳实践是从最高级别目录中的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正在Python目录中查找此文件,但没有找到它。您必须指定您所指的是您所在的目录,而不是Python目录。

要执行此操作,请更改此项:

从application.app.folder.file导入func_name

为此:

from .application.app.folder.file import func_name

通过添加点,您可以在这个文件夹中查找应用程序文件夹,而不是在Python目录中查找。

我多次遇到同一个问题,所以我想分享我的解决方案。

Python版本:3.X

以下解决方案适用于在Python 3.X版本中开发应用程序的人,因为自2020年1月1日以来,Python 2不受支持。

项目结构

在python3中,由于隐式命名空间包,项目子目录中不需要__init__.py。请参见Python 3.3中的包是否不需要init.py+

Project 
├── main.py
├── .gitignore
|
├── a
|   └── file_a.py
|
└── b
    └── file_b.py

问题陈述

在file_b.py中,我想在文件夹a下的file_a.py中导入一个类a。

解决

#1快速但肮脏的方式

不安装软件包,就像您当前正在开发一个新项目

使用try-catch检查是否存在错误。代码示例:

import sys
try:
    # The insertion index should be 1 because index 0 is this file
    sys.path.insert(1, '/absolute/path/to/folder/a')  # the type of path is string
    # because the system path already have the absolute path to folder a
    # so it can recognize file_a.py while searching 
    from file_a import A
except (ModuleNotFoundError, ImportError) as e:
    print("{} fileure".format(type(e)))
else:
    print("Import succeeded")

#2安装软件包

安装应用程序后(在本文中,不包括安装教程)

你可以简单地

try:
    from __future__ import absolute_import
    # now it can reach class A of file_a.py in folder a 
    # by relative import
    from ..a.file_a import A  
except (ModuleNotFoundError, ImportError) as e:
    print("{} fileure".format(type(e)))
else:
    print("Import succeeded")

编码愉快!