Forum Archive

Importing NLTK into the XCode Template

bernard778

I'm new to Pythonista and have started to build an app using the PythonistaAppTemplate.

I'd like to import NLTK, but that isn't in the PythonistaKit.framework.

Is it possible to install NLTK (or other) Python packages into my project and, if so how?

Many thanks in advance...

caddtec

Hi @bernard778 ,
Actually it is possible and there is a lot of ways you can do that. You can use simple python code to download zip files from either github or Pypi for example and then extracting those files into your directories in Pythonista. Also, you can use the github API with the Jose package (that is one of the capacity that I use) to download github sources. And obviously there is Stash that you can use as there is pip with it. As for PyPi, here’s an example of code that I used in the past :

import urllib.request
import tarfile
import shutil
import console
import os
import shutil
import requests

class Installer(object):
    def __init__(self, name, version, module_name=None):
        self.name = name
        self.version = version
        self.module_name = self.name.lower() if not module_name else module_name
        self.tarfolder = self.name + '-' + self.version
        self.tarname = self.tarfolder + '.tar.gz'
        self.tarURL = None
        self.keep_source = False
        self.keep_tarball = False

    def install(self, module_name=None, keep_source=False, keep_tarball=False):
        self.keep_source = keep_source
        self.keep_tarball = keep_tarball
        if module_name:
            self.module_name = module_name
        try:
            self.prepare()
            self.download()
            self.extract()
            self.copy()
        except Exception as e:
            print(str(e))
        finally:
            self.clean()

    def prepare(self):
        r = requests.get('https://pypi.python.org/pypi/{}/{}/json'.format(self.name, self.version))
        jdata = r.json()
        self.tarURL = jdata['urls'][-1]['url']
        print('URL used : {}'.format(self.tarURL))

    def download(self):
        print('Downloading {} v{}...'.format(self.name, self.version))
        if self.tarURL:
            with urllib.request.urlopen(self.tarURL) as response, open(self.tarname, 'wb') as ofile:
                shutil.copyfileobj(response, ofile)
        print('Download Complete')

    def extract(self):
        print('Extracting...')
        t = tarfile.open(self.tarname)
        t.extractall()
        print('Package extracted')

    def copy(self):
        # If source is a folder
        if os.path.isdir(self.tarfolder + '/' + self.module_name):
            if os.path.isdir(self.module_name):
                print('Removing old package directory...')
                shutil.rmtree(self.module_name)
            print('Installing package directory...')
            shutil.move(self.tarfolder + '/' + self.module_name, './' + self.module_name)
        else:
            # if source is a file
            file = self.module_name + '.py'
            if os.path.isfile(self.tarfolder + '/' + file):
                if os.path.isfile(file):
                    print('Removing old package file...')
                    os.remove(file)
                print('Installing package file...')
                shutil.move(self.tarfolder + '/' + file, './' + file)

    def clean(self, keep_module=True):
        print('Cleaning up...')
        if not self.keep_source:
            if os.path.isdir(self.tarfolder):
                print('Removing source directory...')
                shutil.rmtree(self.tarfolder)
        if not self.keep_tarball:
            if os.path.isfile(self.tarname):
                print('Removing source tarball...')
                os.remove(self.tarname)
        if not keep_module:
            if os.path.isdir(self.module_name):
                print('Removing module directory...')
                shutil.rmtree(self.module_name)
            elif os.path.isfile('{}.py'.format(self.module_name)):
                print('Removing module file...')
                os.remove('{}.py'.format(self.module_name))
        print('Done.')

Enjoy!

D