Library/module for Editorial via Pythonista?
-
Hi. I'm experimenting with logging items to a Google spreadsheet from Editorial. I'd like to play with a Google Sheets API library— either Gspread or Pygsheets, but as far as I understand it, I won't be able to install those libraries in Editorial (is that right?) so I'm wondering whether there's a way of doing that setting up the necessary modules in Pythonista and then writing an Editorial workflow that somehow calls on them? Is that even possible?
-
Ah— understood, and good to bear dependencies like that in mind for future reference.
I've now got the gspread folder (contents of the folder in gspread-master) in site-packages/gspread. Still getting an import error (no module named gspread)...
-
You want the path to be:
site-packages/gspread/{files}
and notsite-packages/gspread/gspread/{files}
.
-
@ccc sorry— I realise that the way I phrased that was unclear. I meant that I have all of the files from
gspread-master/gspread/
insite-packages/gspread/
(as you're saying it should be).Admittedly, my first use of
shutil.move
ended up depositing pygsheets files into the folder above site-package— the less said about that, the better... :) That said, I spent a little time tidying up my handling of paths, and I'm pretty sure I had everything in the right place before my final tests of pygsheets, and now gspread. Anything else it might be?Thanks for all the assistance thus far...
-
@jsamlarose47 Could you try restarting Editorial? Not 100% sure why right now, but it seems to help.
-
So... In
site-packages/gspread
you have these files? https://github.com/burnash/gspread/tree/master/gspreadYou probably figured this part out already but if not...
If you are used to using the unix command line then you can vaguely simulate that with https://github.com/cclauss/Ten-lines-or-less/blob/master/cd_ls_pwd.py
For example, in Editor Python Console:
>>> print(os.getcwd()) # pwd in unix /private/var/mobile/Containers/Data/Application/{UUID}/Documents >>> print(os.listdir('.')) # ls in unix ['dear_Sally.pdf', ..., ...] >>> os.chdir('..') # cd in unix >>> print(os.getcwd()) /private/var/mobile/Containers/Data/Application/{UUID} >>> print(os.listdir('.'))
-
See: https://forum.omz-software.com/topic/1438/own-python-modules
http://www.editorial-workflows.com/workflow/5841416039170048/ypL54IX63JY and http://www.editorial-workflows.com/workflow/5218285272432640/U8H7QaIxaJI might help yo get more Python functionality working in Editorial
-
@ccc Yep!
print os.listdir("site-packages/gspread") returns:
['__init__.py', 'client.py', 'exceptions.py', 'gspread', 'httpsession.py', 'models.py', 'ns.py', 'urls.py', 'utils.py']
print os.getcwd() returns:
/private/var/mobile/Containers/Data/Application/{UUID}/Library/Application Support/Commands
Presuming I'm in the right place there...
@olemoritz restarted, no joy...
And of course, the longer this continues the more I think I've just made a rudimentary error somewhere along the way... ;)
-
Can you please try putting the code in
~/Documents/site-packages/
?
-
@jsamlarose47 A short explanation about the
~
part:- (If you've used a Unix shell before, you probably know this part already)
~
means "the user's home directory". That's where on most systems you have folders like "Documents", "Downloads", "Desktop", "Music", etc. - On iOS, you don't have a "home directory", because every app runs in its own sandbox. So on iOS, the "home directory" is the app's user data folder, which is the
/private/var/mobile/Containers/Data/Application/{UUID}
folder that you see in many paths. - Unlike a shell, Python doesn't automatically understand the
~
shortcut. You need to manually useos.path.expanduser(path)
to replace the~
with the actual home directory path.
Long story short, to move your module files to the correct location, this command should work:
shutil.move(os.path.expanduser("~/Library/Application Support/Commands/site-packages/gspread"), os.path.expanduser("~/Documents/site-packages/gspread"))
- (If you've used a Unix shell before, you probably know this part already)
-
Done. Looks like this is the fix for me. Haven't yet fully tested a connection to a spreadsheet, but at least I'm not getting any errors when trying to call gspread modules. Excellent. Thanks!
-
Epilogue!
A couple of questions for me to wrap this up:
1: Just so I've got this down for future reference, should my original script have been something more like the following?
#coding: utf-8 import workflow import urllib import zipfile import shutil import os # get the files url = 'https://github.com/burnash/gspread/archive/master.zip' print "downloading..." urllib.urlretrieve(url, 'gspread.zip') # expand the zip zip_ref = zipfile.ZipFile('gspread.zip', 'r') zip_ref.extractall() zip_ref.close() # move the required folder to the appropriate location shutil.move(os.path.expanduser("~/Library/Application Support/Commands/gspread-master/gspread"), os.path.expanduser("~/Documents/site-packages/gspread"))
Do I need to make the folder in ~/Documents/site-packages/ before I attempt to move something to it?
2: Is it easier to do this kind of work with Pythonista (stash/pip)? And if so, are modules installed via Pythonista also available to Editorial— does Pythonista also look at ~/Documents/site-packages/ or do they function independently?
Thanks again, all!
-
#coding: utf-8 import os import shutil import urllib import workflow import zipfile # get the files url = 'https://github.com/burnash/gspread/archive/master.zip' pkg_name = url.split('/')[4] # gspread zip_name = pkg_name + '.zip' print("downloading...") urllib.urlretrieve(url, zip_name) print("extracting...") with zipfile.ZipFile(zip_name) as in_file: in_file.extractall() srce_dir = os.path.join(os.getcwdu(), pkg_name + '-master', pkg_name) dest_dir = '~/Documents/site-packages' if not os.path.exists(dest_dir): os.makedirs(dest_dir) print('moving {} to {}...'.format(pkg_name, dest_dir)) shutil.move(srce_dir, dest_dir) print('done.')