Create global variables in pythonista_startup
Hi guys, I can't create global variables using pythonista_startup script (that is every time I start Pythonista). Maybe it is an easy thing for you...maybe I can't remember the solution...
pythonista_startup.pyscript for test on global variables is:
def test_global(): global x x = 10 print(x) test_global() global y y=200 print(y)
Procedure to test the script:
- exit Pythonista
- open Pythonista and wait
- check printed x and y in console
- write 'x+y' in console to see output '210'
point 4. fails and returns : name 'x' is not defined
- Pythonista v3.1
- python 2.7.12 interpreter as default
- sorry for this
- my pythonista_startup.py is inside 'site-packages-2'
Can you help me in creating global variables using pythonista_startup?
In S2, what happens if you put
global varbefore you use it?
I think that it is the expected behaviour. Executing in console is different from executing the script. I think that when you execute a script, existing global variables are removed before start running the script but the global variables are not removed or cleared after the script is run. If you run a script from console global variables are not removed or cleared unless you set the configuration to clear the global variables before running. That is the reason why you see the global variables of previously run script. If you run a new script from outside old global variables are removed before running the new script.
@ccc Hi, the problem is not solved with
global varat the beginning of script S2. It says: name var not defined.
But do you have the same problem with Pythonista you use?
@enceladus Hi, thank you for info, but if I want to keep alive the globals I create with different scripts execution, how can I do?
I'm trying to create a global var with execution of a script and another global var with execution of another script in ordert to use the two global variables in python console.
I do not think that it is possible to keep the global variables live across sesssions. I think you need to save them in a file before exiting and load them from the file at the start of the next session. May be you can use json for this.
@enceladus Hi, interesting, thank you for idea. So the solution could be to write a piece of code to insert at the end of any script in order to create a file with content of all global variables and this file is opened by interpreter to read content every time I switch to console window in Pythonista, not only when I launch Pythonista (using pythonista_startup).
Do you have a better idea or an existing working solution created by someonelse?
I think that if your global variable name begins with double underscore, it will not be cleared.
To be tested
Thanks @cvp . Global variables whose names start with double underscores seem to be alive across sessions.
You can also initialise these double underscore global variables in pythonista_startup as suggested by @JonB.
sys.modules['__main__'].__var = 2
There is another option, which is to disable global clearing, using @dgelessus 's pythonista startup
and uncomment the section indicated. This replaces the pythonista_startup with a fake module that lists all globals, thus preventing clearing.
So, synthesizing, a solution for my problem is a mix of double
__at the beginning of var name and the use of
sys.modules['__main__'].__varname=__varnamein external script executed by a user key defined by @JonB script
For those interested, in order to use this script as
pythonista_startupwith the global variables creation feature using user keys, the procedure is the follow:
- At line 181 of this script in place of
def a(sender) print('action from bar1')
def a(sender) import sys, os doc_path = os.path.expanduser('~/Documents/tests') #in folder 'tests' I have my external script to execute by touching user key of JonB script os.chdir(doc_path) sys.path.append(doc_path) execfile('my-script-a.py') # 'my-script-a.py' is inside folder '/tests' app.keyWindow().rootViewController().showAccessoryWithAnimationDuration_(0.5)
- Then create
~/Documents/testswith following content (for example):
import sys __var = 100 sys.modules['__main__'].__var=__var print(__var)
Well, now when you touch user key of
StatusBarMenurelated to action a, Pythonista should execute the script
my-script-a.pyand the variable
__varlives in console also after execution of other external scripts and the var can be used inside other external scripts, as it is 100% global now.
My next step is to adapt this solution for sage_interface in order to be able, using user keys with this, to execute different scripts with remote server by sagemathcell (with scipy and other tools not present in Pythonista) and to use global variables, with different names similar to filename of the executed script by sage (to not overwrite content in variables that could be useful during further calculations), in console or in other scripts. For example, if I must execute remotely with sage a script named 'optimization.py' that calculates A and an other script named 'fit.py' that calculates B, the global variables with output of A and B live in console and the name of the global variables could be '__sage-out--optimization' and '__sage-out--fit', and with the powerful word completion feature of Pythonista them can be recalled easly from python console.
@Matteo If you use the DirAllTheGlobals approach, you can avoid using double underscores, though might still need to set sys.modules['main'].varname from r anythingyou want to save from within startup itself.
@JonB Hi JonB, i will try with option you have described, when finish i will post here a working solution,
https://docs.python.org/3/library/os.html#os.environ and https://docs.python.org/3/library/os.html#os.getenv might also be worth looking into.
@ccc Thank you ccc, I will try to read these info.
check this one...how to share python global variable