我对 python 很陌生,一直在研究数据验证程序。我正在尝试运行我的 main.py 文件,但出现以下错误。
Traceback (most recent call last):
File "/Users/user/data_validation/main.py", line 5, in <module>
from src.compile_csv import combine_csv_files
File "/Users/user/data_validation/src/compile_csv.py", line 5, in <module>
from helpers.helper_methods import set_home_path
ModuleNotFoundError: No module named 'helpers'
我使用的是 python 版本 3.94
这是我的文件夹结构
data_validation
- src
- files
validation.csv
- folder_one
combined.csv
- folder_two
combined_two.csv
- helpers
helper_methods.py
compile_csv.py
mysql.py
split.py
main.py
compile_csv.py
和 split.py
都使用来自 helpers.helper_methods.py
的方法
我的 main.py
在运行时抛出错误,如下所示:
import os
import sys
import earthpy as et
from src.mysql import insert_data, output_non_matches
from src.compile_csv import combine_csv_files
from src.split import final_merged_file
from src.helpers.helper_methods import set_home_path, does_file_exist
home_path = et.io.HOME
file_path = home_path, "data_validation", "src", "files"
folder_name = sys.argv[1]
def configure_file_path():
master_aims_file = os.path.join(
file_path, "validation.csv")
output_csv = os.path.join(
file_path, "output.csv.csv")
gdpr_file_csv = set_home_path(folder_name + "_output.csv")
output_csv = does_file_exist(os.path.join(
file_path, folder_name + "_mismatch_output.csv"))
return output_csv, master_aims_file, gdpr_file_csv
output_csv, master_aims_file, gdpr_file_csv = configure_file_path()
if __name__ == "__main__":
print("? Finding names which do not match name in master file")
if (folder_name == "pd") and (folder_name == "wu"):
final_merged_file()
else:
combine_csv_files()
insert_failures = insert_data(output_csv, master_aims_file)
output_failures = output_non_matches(output_csv, gdpr_file_csv)
if insert_failures or output_failures:
exit(
"⚠️ There were errors in finding non-matching data, read above for more info"
)
os.remove(os.path.join(home_path, "data_validation", "members_data.db"))
exit(
f"✅ mismatches found and have been outputted to {output_csv} in the {folder_name} folder")
根据我在 python 3 中的理解,我们不需要使用 __init__.py
,您可以使用 .
在导入过程中定义路径,所以我不完全确定我做错了什么.
我正在从 /Users/user/data_validation
执行文件并使用以下命令 python main.py pd
答案 0 :(得分:1)
就是这样。错误发生在您的 compile_csv.py
文件中。我猜在那个文件中你有 from helpers.helper_methods import blah
。但是你需要把它改成
from .helpers.helper_methods import blah
OR
from src.helpers.helper_methods import blah
原因是导入与 cwd 相关,而不是与运行代码的文件相关。因此,您需要添加相对于 /Users/user/data_validation
的导入。
答案 1 :(得分:0)
您可以通过设置 PYTHONPATH 变量来尝试。查看更多相关信息here