简体   繁体   English

覆盖函数内的全局变量不适用于 Spyder 4

[英]Override global variable inside function not working with Spyder 4

I try to override globally defined Dataframes from within a function.我尝试从函数中覆盖全局定义的数据帧。 Somehow the global values do not change, printing the dataframes within the functions works with the expected values.不知何故,全局值不会改变,在函数内打印数据帧与预期值一起工作。

import pandas as pd

rawData = pd.read_csv("music.csv")
appTitles =pd.DataFrame #also with pd.DataFrame() wont get affected

def loadTitles(df=rawData):
    global appTitles
    appTitles = pd.DataFrame({'Title' : df['title']})
    print(appTitles) 
return appTitles #(second 'print' to screen)

#RUN FILE
#CALL FUNCTION: loadTitles() #-> No Variable appTitles

The appTitles variable in the variable explorer remains empty or nonexistent (depending use of pd.DataFrame/pd.DataFrame()) Thank you!变量资源管理器中的 appTitles 变量保持为空或不存在(取决于 pd.DataFrame/pd.DataFrame() 的使用)谢谢!

EDIT: After @4turkuaz commented that on his environment everthing works as expected, I tried Python console within Windows cmd and also on Spyder3 with Python 3.7.4 & IPython 7.8.0 where also everything worked fine.编辑:在@4turkuaz 评论说在他的环境中一切都按预期工作后,我在 Windows cmd 中尝试了 Python 控制台,还在使用 Python 3.7.4 和 IPython 7.8.0 的 Spyder3 上尝试了 Python 控制台,其中一切正常。

Only on Spyder4 with Python 3.7.5 & IPython 7.10.1 it won't work.仅在带有 Python 3.7.5 和 IPython 7.10.1 的 Spyder4 上它不起作用。 Maybe a bug?也许是一个错误? I don't know...我不知道...

EDIT: Downgrading Spyder4 to Python 3.7.4 & IPython 7.8.0 did not solve the problem.编辑:将Spyder4 降级到 Python 3.7.4 & IPython 7.8.0 并没有解决问题。 I'm assuming that something within Spyder4 or the Anaconca Environment even though I installed it over and over is not correct.我假设 Spyder4 或 Anaconca Environment 中的某些东西即使我一遍又一遍地安装它是不正确的。

( Spyder maintainer here ) You need to go to the menu 这里是 Spyder 维护者)你需要去菜单

Run > Configuration per file

and activate the option called Run in console's namespace instead of an empty one , for you to be able to modify global variables in your code.并激活名为Run in console's namespace instead of an empty one的选项,以便您能够修改代码中的全局变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM