Forum Archive

Error when trying to import third-party module from iOS Sharing extension

naturale

Recently, I installed nltk with StaSh to do some natural language stuffs.

I wrote a python 2 script with pythonista, which works from iOS sharing extension.

But whenever I tried to run the script from sharing extension, an error occurred:
ImportError: No module named nltk, even though nltk was installed at /Documents/Modules & Templates/site-packages.

When I try to import the nltk from the Pythonista3 app, it was imported normally without any bugs.

My default interpreter is python 2 now.

I searched the community for the phenomenon but got nothing about this bug.

Anyone know how to fix this?

Thanks.

ccc

In a Pythonista 3 installation there are two site-packages folders. One is for Python 2 and the other for Python 3. When you pip install, you are installing a package for one version for Python but not for both.

naturale

I thought nltk was installed at site packages 2, but found in 3. Thanks!

JonB

This appears to be a bug in the extension for Pythonista 3... at least the latest beta. if you go the the extension console, and look at sys.path[0] it is pointing to the PluginKit folder, rather than the shared AppGroup. This was the old state of affairs which was fixed in the app store release, I thought.

This will work as a lead in to scripts that want to import from site-packages

import sys
import os
try:
    import ntlk
except ImportError:
    sys.path.insert(0,os.path.join(os.getcwd(),'site-packages'))
    import nltk

(this would work for files in the main Documents folder, otherwise you need to os.path.split your way up to Documents)

To ccc's point- stash pip installs into site-packages by default, not the version specific folders. So, you should be able to import from either python version, assuming the code is compatible. Though in some cases, since stash runs in the py2 interpreter, setuptools might end up doing some stupid things for packages that are too smart for their own good, in which case you have to download and manually install. But this is not one of those cases

omz

@JonB Thanks for the bug report!