Python NLTK align import error - python

I am getting a strange import error for NLTK's align module:
$ python2 --version
Python 2.7.10
$ pip2 freeze | grep nltk
nltk==3.2
$ python2
Python 2.7.10 (default, Oct 23 2015, 18:05:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>> from nltk.align import AlignedSent
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named align
As you can see, I have nltk 3.2 installed for python 2.7. I can import nltk directly. However, I cannot access the align module.
I know the import statement I used is correct, since I took it directly from the official documentation. Additionally, I looked at the interface changes from NLTK 2 to NLTK 3 and there is no mention there of the align module.
Why is this happening and what can I do to fix this?

As of NLTK version 3.2, the align module has been renamed to translate. Therefore use:
from nltk.translate import AlignedSent

Related

I can't import 'NSWorkspace' from 'AppKit'

I've got a problem when I try to import NSWorkspace an error appears:
No name 'NSWorkspace' in module 'AppKit'
Here is my code :
from AppKit import NSWorkspace
import time
activeAppName = ""
while True:
NewactiveAppName = NSWorkspace.sharedWorkspace().activeApplication()['NSApplicationName']
if activeAppName != NewactiveAppName:
activeAppName = NewactiveAppName
print (activeAppName)
time.sleep(10)
Python v3 does not import module AppKit with capital letters but requires lower ones:
import appkit
So, in my case, the original way works with only Python v2.
When I tried Python 3, it produced the following error:
Python 3.9.1 (default, Dec 17 2020, 03:41:37)
[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import AppKit
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'AppKit'
>>>
When I use python 2.7 — it works fine:
WARNING: Python 2.7 is not recommended.
This version was included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7. Instead, it is recommended using 'python3' from within Terminal scripts.
Python 2.7.16 (default, Mar 25 2021, 03:11:28)
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin
Type "help", "copyright", "credits" or "license" for more information.
import AppKit
>>
NO ERROR.
Definitely, I have tried the recommended pip3 install AppKit PyObjC to no avail.
There is a recommendation that I didn't try.
So, I stick to use Python2 with the following script:
#!/usr/bin/python
import logging
logging.basicConfig(filename='/tmp/act_window.log', level=logging.INFO, format='%(asctime)s - %(
try:
from AppKit import NSWorkspace
activeAppName = NSWorkspace.sharedWorkspace().activeApplication()['NSApplicationName']
print(activeAppName)
logging.info(activeAppName)
except Exception as e:
logging.error("{0}".format(e))
print("Unknown")
————
UPDATE:
I have manually updated my AppKit library for python 3 and now use it in Python3-based script.
https://github.com/TinKurbatoff/appkit
2022 Update:
Per this post, pip3 install pyobjc made it possible to from AppKit import NSWorkspace (using Python 3.10.7)

Cannot import any modules (installed used pip) in python 2.7.14

I downloaded the source code of python 2.7.14 and built it and installed it on linux ( Red Hat 4.8.5-16 ). I have earlier installed python-magic and requests libraries. Now when I try to import modules installed using pip, I get this:
$ python2
Python 2.7.14 (default, Nov 9 2017, 09:05:45)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import magic
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named magic
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named requests
while similar thing works perfectly fine in python 2.7.5 (default with the RHEL system)
$ python
Python 2.7.5 (default, May 3 2017, 07:55:04)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-14)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import magic
>>> import requests
>>>
Am I missing any configuration step here?
The modules installed via pip are only available to the standard python version. You need to install your desired packages for the non-standard python versions as well (see also Installing Python Modules)
python2 -m pip install python-magic
python2 -m pip install requests
your pip is default set for version
$ python
Python 2.7.5
SO if you want to install module for
$ python2
Python 2.7.14
use python2 -m pip install module_name
The sys.path must be different for the two installations. That's a list of folders that Python checks for imports.
It's probably simplest to just symlink the one for wherever pip installs to into the modules folder for 2.7.14.

How to bind openalpr with python on a mac?

I recently installed openalpr on my mac using brew install openalpr with success. I would like to use the openalpr library with python 2.7 but I am having difficulty binding the two and could use some help.
I currently get the following in my projects file location:
Python 2.7.11 (default, Jan 22 2016, 08:29:18)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from openalpr import Alpr
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named openalpr
>>>
When I move to /usr/local/Cellar/openalpr/2.3.0/lib/python2.7/dist-packages/openalpr
The import works. How can I bind this library? Thanks
EDIT: I think I've seen that running the setup.py for openalpr is how you bind but I have no idea where to find it in my file system.
I had to do the following command:
echo /usr/local/opt/openalpr/lib/python2.7/dist-packages/ >> /usr/local/lib/python2.7/site-packages/openalpr.pth

Python version/import confusion

I'm trying to get this Python 2.7 code to work.
https://github.com/slanglab/phrasemachine
I've downloaded and unzipped the repo from github. Here's what happens when I try to run the code.
phrasemachine$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import phrasemachine
>>> text = "Barack Obama supports expanding social security."
>>> print phrasemachine.get_phrases(text)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "phrasemachine.py", line 253, in get_phrases
tagger = TAGGER_NAMES[tagger]()
File "phrasemachine.py", line 166, in get_stdeng_nltk_tagger
tagger = NLTKTagger()
File "phrasemachine.py", line 133, in __init__
import nltk
ImportError: No module named nltk
So, I need the nltk module. I have that installed here:
Sure enough, Python 2 doesn't know about nltk.
phrasemachine$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named nltk
But, Python 3 does.
phrasemachine$ python3
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>>
Pip tells me that nltk is already installed, but for 3.5.
$ sudo pip install -U nltk
Requirement already up-to-date: nltk in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk-3.2.1-py3.5.egg
Update 10/10/16: I installed the 2.7 version of Python via brew, which give me the 2.7 pip.
$ /usr/local/bin/pip --version
pip 8.1.2 from /usr/local/lib/python2.7/site-packages (python 2.7)
Then I installed nltk with that pip:
$ sudo /usr/local/bin/pip install -U nltk
Password:
The directory '/Users/me/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/me/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting nltk
Downloading nltk-3.2.1.tar.gz (1.1MB)
100% |████████████████████████████████| 1.1MB 683kB/s
Installing collected packages: nltk
Running setup.py install for nltk ... done
Successfully installed nltk-3.2.1
It says it installed nltk but the warnings are concerning. And, Python 2.7 still fails to import nltk.
$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import phrasemachine
>>> text = "Barack Obama supports expanding social security."
>>> print phrasemachine.get_phrases(text)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "phrasemachine.py", line 253, in get_phrases
tagger = TAGGER_NAMES[tagger]()
File "phrasemachine.py", line 166, in get_stdeng_nltk_tagger
tagger = NLTKTagger()
File "phrasemachine.py", line 133, in __init__
import nltk
ImportError: No module named nltk
>>> import nltk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named nltk
Final update! I pointed Python 2.7 to the site packages directory where Homebrew installs stuff and I'm now good!
>>> import sys
>>> sys.path.append('/usr/local/lib/python2.7/site-packages')
Since you have two python distributions, you also need two versions of pip. Find out where your pip executables are with which -a pip, and install pip for your Python 2.7 distribution if necessary. Then tell the pip that goes with Python 2.7 (perhaps /usr/local/bin/pip) to install the nltk.
(Edit: Pip must be able to find the proper Python on its PATH. I hadn't thought to go into this.)

I cannot import igraph on python 2.6 after installation

I installed igraph for python 2.6 on OSX 10.7, but I cannot import igraph library.
It shows a import error:no module name igraph.
I have no idea. Please help me out.
Thanks.
However, I cannot run code with python. I make a code, and tried to run, such as, 'python ex.py'
OK, even though you didn't answer most of my questions, I'm pretty sure I can guess your problem. Your question title is "I cannot import igraph on python 2.6 after installation", but you're not trying to import it on python 2.6, you're trying on 2.7.
$ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import igraph
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named igraph
>>>
$ python2.6
Python 2.6.7 (r267:88850, Jun 20 2012, 16:23:38)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import igraph
>>>
See the difference? OS X 10.7 (and 10.8) comes with three versions of Python: 2.5, 2.6, and 2.7. They're entirely independent installations, so when you installed igraph for Python 2.6, that didn't install it for your 2.5 or 2.7 installations.
The default, the one you get when you just run python, is 2.7. If you want a specific version, you have to run python2.6 instead.
So, you either need to run python2.6, or install igraph for 2.7.
As a side note, if you've installed any third-party Python installations, you're going to get yourself even more confused, so please, don't do that (or uninstall if you already have) until you really know what you're doing.

Categories