我有一个如下所示的目录树:
lib -
|
__init__.py
|
settings.py
|
helpers-
|
space_help.py
|
__init__.py
我需要将settings.py
中的内容导入space_help.py
,但是,当我尝试导入并运行它时(使用python 2.7 ),我收到以下错误:
Traceback (most recent call last):
File "dre234.py", line 10, in <module>
from lib.settings import CLONE
File "C:\Users\me\Documents\bin\python\dre234\lib\settings.py", line 9, in <module>
from lib.algorithms.step_up import *
File "C:\Users\me\Documents\bin\python\dre234\lib\algorithms\space_help.py", line 9, in <module>
from lib.settings import random_salt_generator
ImportError: No module named settings
为什么我无法将其导入文件?
答案 0 :(得分:0)
当您尝试使用python导入内容时,它首先检查当前模块的目录,然后检查系统路径。如果要从当前目录上方的文件夹导入某些内容,则需要将其添加到系统路径中。像这样:
import os
import sys
file_path = os.path.join(os.path.dirname(__file__), '..')
sys.path.append(file_path)
from settings import CLONE, random_salt_generator
from algorithms.step_up import *