Override global variable inside function not working with Spyder 4

久未见 提交于 2020-06-21 05:38:34

问题


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!

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.

Only on Spyder4 with Python 3.7.5 & IPython 7.10.1 it won't work. Maybe a bug? I don't know...

EDIT: Downgrading Spyder4 to Python 3.7.4 & IPython 7.8.0 did not solve the problem. I'm assuming that something within Spyder4 or the Anaconca Environment even though I installed it over and over is not correct.


回答1:


(Spyder maintainer here) You need to go to the menu

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.



来源:https://stackoverflow.com/questions/59271717/override-global-variable-inside-function-not-working-with-spyder-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!