创建于:20201204
修改于:20201204,20211104
1、背景
在PyCharm下的Terminal运行文件或者模块。出现了错误:ModuleNotFoundError: No module named ‘src’
2、解决方案
2.1 增加系统路径(测试成功)
sys.path.insert(0, ‘…/’)
import sys, os
sys.path.insert(0, '../')
from setup import LOGFILE, NOW
from src.model import health_index
if __name__ == '__main__':
print("Start at ", NOW, ", 输入内容为:", sys.argv)
LOGFILE.info(sys.argv)
for arg in sys.argv:
print("", arg)
在PyCharm下的Terminal下运行及结果如下:
(eehi366) D:\programs\Anaconda_program\eehi\src>python main.py 1,2,3 a.out
file_name = log/eehi.log
Start at 2020-12-04 15:46:31.101098 , 输入内容为: ['main.py', '1,2,3', 'a.out']
main.py
1,2,3
a.out
sys.path.insert(0,"/path") 的用法
2.2 采用module的方式运行(测试失败)
注意:此时没有sys.path.insert(0, ‘…/’)行代码
(eehi366) D:\programs\Anaconda_program\eehi>python -m src.main 202010 test_out
file_name = log/eehi.log
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\envs\eehi366\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\ProgramData\Anaconda3\envs\eehi366\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "D:\programs\Anaconda_program\eehi\src\main.py", line 25, in <module>
from src.model import health_index
File "D:\programs\Anaconda_program\eehi\src\model.py", line 15, in <module>
LOGFILE.info("模型输入表的表头是\n" + str(list(input_data().columns)) + "\n\n")
File "D:\programs\Anaconda_program\eehi\src\io.py", line 27, in input_data
encoding='GBK', usecols=cols, nrows=rows)
File "C:\ProgramData\Anaconda3\envs\eehi366\lib\site-packages\pandas\io\parsers.py", line 686, in read_csv
return _read(filepath_or_buffer, kwds)
File "C:\ProgramData\Anaconda3\envs\eehi366\lib\site-packages\pandas\io\parsers.py", line 452, in _read
parser = TextFileReader(fp_or_buf, **kwds)
File "C:\ProgramData\Anaconda3\envs\eehi366\lib\site-packages\pandas\io\parsers.py", line 936, in __init__
self._make_engine(self.engine)
File "C:\ProgramData\Anaconda3\envs\eehi366\lib\site-packages\pandas\io\parsers.py", line 1168, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "C:\ProgramData\Anaconda3\envs\eehi366\lib\site-packages\pandas\io\parsers.py", line 1981, in __init__
src = open(src, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '../data/model_input.csv'
Python3.6 error: ModuleNotFoundError: No module named ‘src’
2.3 自建的module包所在路径不在PYTHONPATH下
在alg环境下,import其他文件报错:ModuleNotFoundError: No module named ‘src’。信息如下。
from src.evaluate_metrics import EvaluateMetrics
from alg.algorithm.Algorithm import Algorithm
File "D:\programs\Anaconda_program\do_time_series_product\src\time_series_ma.py", line 19, in <module>
from src.evaluate_metrics import EvaluateMetrics
ModuleNotFoundError: No module named 'src'
尝试过下面的3种,没有解决。
- (1)仅仅增加,sys.path.insert(0, ‘…/src’),无法解决
- (2)在下面的语句中追加sys.path.append(curPath),无法解决。
curPath = os.path.abspath(os.path.dirname(file))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)
sys.path.append(curPath)
- (3)以上文件写到evaluate_metrics.py文件中,也无法解决。
改成下面的就解决了,顺序不能颠倒。其含义是:把当前文件所在文件夹的父文件夹路径加入到PYTHONPATH。
先把文件路径追加到系统变量中,再导入包。顺序不可颠倒。
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from src.evaluate_metrics import EvaluateMetrics
# 或者
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)
from src.evaluate_metrics import EvaluateMetrics
下面这个解决方案大全,非常好,建议好好读。
ModuleNotFoundError: No module named 'xxx’可能的解决方案大全