Python 尝试 运行 main.py 时出现 ModuleNotFoundError
Python ModuleNotFoundError when trying to run main.py
我是 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
您可以尝试设置 PYTHONPATH 变量。查看更多信息 here
就是这样。错误发生在您的 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
.
的导入
我是 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
您可以尝试设置 PYTHONPATH 变量。查看更多信息 here
就是这样。错误发生在您的 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
.