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

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)

Related

How to install lirc in Python 3.8 when previously installed in Python 3.5?

I have been using lirc in Python 2.7 and Python 3.5.3 on Rpi3. Now I have installed Python 3.8.7 from source code, but I am not able to use lirc in this version of Python, but it still works in Python 3.5. apt-get reports that python3-lirc is already the newest version (1.2.1-2). How to get lirc work in both Python 3.5 and 3.8?
pi#RPi3:~ $ python
Python 3.8.7 (default, Jan 7 2021, 08:59:27)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import lirc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'lirc'
>>>
pi#RPi3:~ $ python3.5
Python 3.5.3 (default, Nov 18 2020, 21:09:16)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import lirc
>>> sockid=lirc.init("myProg", blocking = False)
>>>
It means that you installed lirc on only one of the two pythons.
Use the pip from your Python 3.8 to install lirc so that the module will also be accessible to your Python 3.8.
Module lirc has to be uninstalled with pip, then python-lirc can be installed in Python 3,8 as described in https://github.com/tompreston/python-lirc/issues/26 after installing cyhton with pip.

Importerror: No module named time in python2.7.6 in ubuntu

I have a strange problem on python2.7.6 with Ubuntu :
my python 2.7.6 is installed but strangely time module is not working. my ubuntu version is 16.04
i tried installing python 2.7.16 as well but that didnt fixed the issue as
/usr/local/bin# python2.7
Python 2.7.6 (default, Jul 22 2019, 12:49:04)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named time
expected : import time should work
actual : ImportError: No module named time
How do i import time in python 2.7.6
output of :
: ls /usr/lib/python2.7/lib-dynload/
audioop.i386-linux-gnu.so _codecs_tw.i386-linux-gnu.so dl.i386-linux-gnu.so _lsprof.i386-linux-gnu.so Python-2.7.egg-info
audioop.x86_64-linux-gnu.so _codecs_tw.x86_64-linux-gnu.so _elementtree.i386-linux-gnu.so _lsprof.x86_64-linux-gnu.so readline.i386-linux-gnu.so
_bsddb.i386-linux-gnu.so crypt.i386-linux-gnu.so _elementtree.x86_64-linux-gnu.so mmap.i386-linux-gnu.so readline.x86_64-linux-gnu.so
_bsddb.x86_64-linux-gnu.so crypt.x86_64-linux-gnu.so fpectl.i386-linux-gnu.so mmap.x86_64-linux-gnu.so resource.i386-linux-gnu.so
bz2.i386-linux-gnu.so _csv.i386-linux-gnu.so fpectl.x86_64-linux-gnu.so _multibytecodec.i386-linux-gnu.so resource.x86_64-linux-gnu.so
bz2.x86_64-linux-gnu.so _csv.x86_64-linux-gnu.so future_builtins.i386-linux-gnu.so _multibytecodec.x86_64-linux-gnu.so _sqlite3.i386-linux-gnu.so
_codecs_cn.i386-linux-gnu.so _ctypes.i386-linux-gnu.so future_builtins.x86_64-linux-gnu.so _multiprocessing.i386-linux-gnu.so _sqlite3.x86_64-linux-gnu.so
_codecs_cn.x86_64-linux-gnu.so _ctypes_test.i386-linux-gnu.so _hashlib.i386-linux-gnu.so _multiprocessing.x86_64-linux-gnu.so _ssl.i386-linux-gnu.so
_codecs_hk.i386-linux-gnu.so _ctypes_test.x86_64-linux-gnu.so _hashlib.x86_64-linux-gnu.so nis.i386-linux-gnu.so _ssl.x86_64-linux-gnu.so
_codecs_hk.x86_64-linux-gnu.so _ctypes.x86_64-linux-gnu.so _hotshot.i386-linux-gnu.so nis.x86_64-linux-gnu.so termios.i386-linux-gnu.so
_codecs_iso2022.i386-linux-gnu.so _curses.i386-linux-gnu.so _hotshot.x86_64-linux-gnu.so ossaudiodev.i386-linux-gnu.so termios.x86_64-linux-gnu.so
_codecs_iso2022.x86_64-linux-gnu.so _curses_panel.i386-linux-gnu.so imageop.i386-linux-gnu.so ossaudiodev.x86_64-linux-gnu.so _testcapi.i386-linux-gnu.so
_codecs_jp.i386-linux-gnu.so _curses_panel.x86_64-linux-gnu.so _json.i386-linux-gnu.so parser.i386-linux-gnu.so _testcapi.x86_64-linux-gnu.so
_codecs_jp.x86_64-linux-gnu.so _curses.x86_64-linux-gnu.so _json.x86_64-linux-gnu.so parser.x86_64-linux-gnu.so
_codecs_kr.i386-linux-gnu.so dbm.i386-linux-gnu.so linuxaudiodev.i386-linux-gnu.so pyexpat.i386-linux-gnu.so
_codecs_kr.x86_64-linux-gnu.so dbm.x86_64-linux-gnu.so linuxaudiodev.x86_64-linux-gnu.so pyexpat.x86_64-linux-gnu.so
It seems time package is not install, you can try by installing time package using below command.
pip install times
or
sudo pip install times.
and then try?

python module import error when running, while imported fine in interactive mode

I've created a clean python3.6 virtualenv in a native python2 environment via anaconda3, and I'm able to import some module in the interactive mode:
$ python
Python 3.6.3 |Anaconda, Inc.| (default, Oct 13 2017, 12:02:49)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from skimage.transform import resize
>>>
however, this seems to break when I run in a non-interactive mode by calling:
(py3)$ python filename.py
...
File "path_to_module.py", line 6, in <module>
from skimage.transform import resize
...
ImportError: /usr/lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by path_to_pyenv/anaconda3/lib/python3.6/site-packages/matplotlib/_path.cpython-36m-x86_64-linux-gnu.so)
Note this error does not occur exclusively by importing this particular skimage module, so i guess this is a generic error.
Anyone has an idea how to fix this?
Thanks!
turns out that the native matplotlib coming with Anaconda was outdated, so i fixed this error by upgrading it:
pip install matplotlib --upgrade

Googleapiclient and python3

I wrote a basic Python 3 script that uses the Google Sheets API. It works on a system that defaults to Python 3 (Arch). I'm trying to run the same script on an Ubuntu 14.04 system, but I'm unable to load the apiclient library. I installed with the recommended
pip install --upgrade google-api-python-client
But I noticed I can only load the library in python 2.
Here's what I'm observing:
~ $ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from googleapiclient import discovery
>>> quit()
~ $ python3
Python 3.4.3 (default, Nov 17 2016, 01:08:31)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from googleapiclient import discovery
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'googleapiclient'
Any advice?
The Googleapiclient is installed only on python2 (Which i guess is your default python version) not python3.
Install Googleapiclient in python3 env using the following:
pip3 install --upgrade google-api-python-client
python -m pip install --upgrade google-api-python-client

Python NLTK align import error

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

Categories