为什么我的python程序没有通过调试器运行时会引发错误?

时间:2019-10-01 00:22:51

标签: python pickle

首先,我使用播放按钮在Pycharm中运行以下代码段:

import os
import pickle
import matplotlib.pyplot as plt
import numpy as np

directory = os.path.join(os.path.dirname(os.path.realpath(__file__)), './linear_models')
file_path = os.path.join(directory, '/factor_model/predict_car_prices.p')
with open(file_path, 'rb') as f:
    lm = pickle.load(f)
print("Cat")

它具有以下追溯:

Traceback (most recent call last):
  File "/Users/ben/Documents/programs/testload.py", line 9, in <module>
    lm = pickle.load(f)
  File "/usr/local/lib/python3.7/site-packages/statsmodels/base/data.py", line 105, in __setstate__
    return_type='dataframe')
  File "/usr/local/lib/python3.7/site-packages/patsy/highlevel.py", line 308, in dmatrices
    eval_env = EvalEnvironment.capture(eval_env, reference=1)
  File "/usr/local/lib/python3.7/site-packages/patsy/eval.py", line 226, in capture
    return cls([frame.f_locals, frame.f_globals],
AttributeError: 'NoneType' object has no attribute 'f_locals'

如果我运行调试器,则程序将完成而不会报告错误。

1 个答案:

答案 0 :(得分:0)

我不确定这是否是根本原因,但是我在您的代码中看到了确定的错误:

file_path = os.path.join(directory, '/factor_model/predict_car_prices.p')

根据documentation,“如果组件是绝对路径,则所有先前的组件都将被丢弃,并且连接将从绝对路径组件继续。”因此,file_path实际上就是'/factor_model/predict_car_prices.p',我认为这是不正确的。