Python ';非类型';对象没有属性';光标';气流
我正在尝试用basic3函数测试Apache气流。并按相同顺序将它们分配给3个任务。但问题是: 当我试图将数据加载到POstgresql db时,它抛出了一个错误,Python ';非类型';对象没有属性';光标';气流,python,function,variables,global-variables,airflow,Python,Function,Variables,Global Variables,Airflow,我正在尝试用basic3函数测试Apache气流。并按相同顺序将它们分配给3个任务。但问题是: 当我试图将数据加载到POstgresql db时,它抛出了一个错误,NoneType对象没有属性游标 有什么建议可以解决吗 代码是: def fetch_data(): global df #Fetch data from url url = 'http://api.open-notify.org/astros.json' response = requests.ge
NoneType对象没有属性游标
有什么建议可以解决吗
代码是:
def fetch_data():
global df
#Fetch data from url
url = 'http://api.open-notify.org/astros.json'
response = requests.get(url)
logging.info(response.status_code)
json_data = response.json()
logging.info(json_data)
#Normalize it
df = json_normalize(json_data['people'])
return df
def connect_to_db():
global engine
# Connect to database (Note: The package psychopg2 is required for Postgres to work with SQLAlchemy)
engine = create_engine("postgresql://postgres:postgres@localhost/postgres")
logging.info('Connected to the database')
# Verify that there are no existing tables
print(engine.table_names())
return engine
def load_data():
engine = connect_to_db()
df = fetch_data()
table_name = 'astro'
logging.info('Table is created')
df.to_sql(table_name, engine)
logging.info('Data is loaded.')
回溯:
ERROR - 'NoneType' object has no attribute 'cursor'
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/airflow/models/taskinstance.py", line 926, in _run_raw_task
result = task_copy.execute(context=context)
File "/usr/local/lib/python2.7/dist-packages/airflow/operators/python_operator.py", line 113, in execute
return_value = self.execute_callable()
File "/usr/local/lib/python2.7/dist-packages/airflow/operators/python_operator.py", line 118, in execute_callable
return self.python_callable(*self.op_args, **self.op_kwargs)
File "/usr/local/lib/python2.7/dist-packages/airflow/example_dags/sample_task.py", line 65, in load_data
df.to_sql(name=table_name, con = engine, if_exists='append')
File "/usr/local/lib/python2.7/dist-packages/pandas/core/generic.py", line 2531, in to_sql
dtype=dtype, method=method)
File "/usr/local/lib/python2.7/dist-packages/pandas/io/sql.py", line 460, in to_sql
chunksize=chunksize, dtype=dtype, method=method)
File "/usr/local/lib/python2.7/dist-packages/pandas/io/sql.py", line 1546, in to_sql
table.create()
File "/usr/local/lib/python2.7/dist-packages/pandas/io/sql.py", line 572, in create
if self.exists():
File "/usr/local/lib/python2.7/dist-packages/pandas/io/sql.py", line 560, in exists
return self.pd_sql.has_table(self.name, self.schema)
File "/usr/local/lib/python2.7/dist-packages/pandas/io/sql.py", line 1558, in has_table
return len(self.execute(query, [name, ]).fetchall()) > 0
File "/usr/local/lib/python2.7/dist-packages/pandas/io/sql.py", line 1426, in execute
cur = self.con.cursor()
AttributeError: 'NoneType' object has no attribute 'cursor'
我已经删除了全局变量,并保持了rest不变,它就工作了
谢谢大家 我不明白为什么需要全局变量。您有赋值
engine=connect\u to\u db()
,因此它从函数中获取值。它不需要是全局的。'NoneType'对象没有属性“cursor”
我得到了这个错误,然后在问题中发布了完整的回溯。我看不出这是怎么发生的,因为您在load_data()
错误中正确分配了引擎-“NoneType”对象没有属性“cursor”回溯(最近一次调用最后一次):File“/usr/local/lib/python2.7/dist packages/afflow/models/taskinstance.py”,第926行,在_run\u raw\u task result=task\u copy.execute中(context=context)File“/usr/local/lib/python2.7/dist packages/afflow/operators/python\u operator.py”,第113行,在execute return\u value=self.execute\u callable()等
中,请将其发布在问题中,以便其格式化为可读。但我没有看到您在回溯中显示的任何函数。