[英]How do i make python 3.6 import files relative to the file location?
我有一个使用以下目录结构编写的简单Python 3.6程序
src/
Main.py
File_Readers/
File_Reader.py
Input_Reader.py
我编写了Input_Reader.py文件,使其具有以下结构。
import File_Reader as fr
def read_finance_history(file_name, path_length)
"""
Function in this section reads in a .csv and
places the data into a dictionary
"""
return Dictionary
class FileReaderTest(unites.TestCase)
def test_read_finance_history(self):
""" Unit test code in this section """
我对File_Readers
目录中的Input_Reader.py
文件进行了单元测试,并且工作正常。 但是,当我通过Main.py
文件中的导入语句调用Input_Reader.py
程序时,它无法识别Input_Reader.py
文件中调用的File_Reader
模块。 显然,代码引用了Main.py
位置中的所有内容,我可以通过更改Input_Reader.py
文件中的import语句以从Input_Reader.py
中读取file来读取from File_Readers import File_Reader as fr
,但是然后我无法从File_Readers
运行单元测试目录。 如何强制Input_Reader.py
文件中的import语句始终相对于该文件的位置进行导入?
from . import File_Reader as fr
使用from . import File_Reader as fr
from . import File_Reader as fr
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.