Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Create global variables in pythonista_startup
-
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
see
https://github.com/dgelessus/pythonista_startup/blob/master/init.pyand uncomment the section indicated. This replaces the pythonista_startup with a fake module that lists all globals, thus preventing clearing.
-
@cvp @enceladus and @JonB , thank you for feedback, it is undoubtedly nice that there is a sharing of ideas and solutions.
So, synthesizing, a solution for my problem is a mix of double
__
at the beginning of var name and the use ofsys.modules['__main__'].__varname=__varname
in external script executed by a user key defined by @JonB scriptStatusBarMenu
.For those interested, in order to use this script as
pythonista_startup
with 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')
insert
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
my-script-a.py
in folder~/Documents/tests
with following content (for example):
import sys __var = 100 sys.modules['__main__'].__var=__var print(__var)
Well, now when you touch user key of
StatusBarMenu
related to action a, Pythonista should execute the scriptmy-script-a.py
and the variable__var
lives 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.
Best regards
-
@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,
Regards -
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.
Regards -
check this one...how to share python global variable