我有这样的文件夹结构:
application
├── app
│ └── folder
│ └── file.py
└── app2
└── some_folder
└── some_file.py
如何从file.py或some_file.py中导入函数?我尝试了:
from application.app.folder.file import func_name
但它不起作用。
我有这样的文件夹结构:
application
├── app
│ └── folder
│ └── file.py
└── app2
└── some_folder
└── some_file.py
如何从file.py或some_file.py中导入函数?我尝试了:
from application.app.folder.file import func_name
但它不起作用。
当前回答
├───root
│ ├───dir_a
│ │ ├───file_a.py
│ │ └───file_xx.py
│ ├───dir_b
│ │ ├───file_b.py
│ │ └───file_yy.py
│ ├───dir_c
│ └───dir_n
您可以将父目录添加到PYTHONPATH,为了实现这一点,您可以在sys.path中列出的“模块搜索路径”中使用依赖于操作系统的路径。因此,您可以像下面这样轻松地添加父目录:
# file_b.py
import sys
sys.path.insert(0, '..')
from dir_a.file_a import func_name
其他回答
我多次遇到同一个问题,所以我想分享我的解决方案。
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")
编码愉快!
据我所知,直接在要导入的函数的文件夹中添加__init__.py文件即可完成此任务。
注:这个答案是针对一个非常具体的问题。对于大多数来自搜索引擎的程序员来说,这不是你想要的答案。通常,您会将文件结构化为包(参见其他答案),而不是修改搜索路径。
默认情况下,您不能。当导入文件时,Python只搜索入口点脚本运行的目录和sys.path,其中包括包安装目录等位置(实际上比这复杂一点,但这涵盖了大多数情况)。
但是,您可以在运行时向Python路径添加:
# some_file.py
import sys
# caution: path[0] is reserved for script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')
import file
创建包的最佳实践是从最高级别目录中的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位于同一目录。
只需使用os模块中的change-dir函数:
os.chdir("Here new director")
比正常情况下可以导入的更多信息