Cannot use Python select.poll in Mac OS? - python

$ python
Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import select
>>> select.poll
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'poll'

Instead of using poll, use select.kqueue() on OSX. It's similar to 'epoll' on Linux in that you can more efficiently register for types of file-descriptor / file system events which can be used in asynchronous code. Much more efficient than polling.
Otherwise, the equivalent is just running a blocking select.select() inside a while True: loop with some sort of timeout?

If you want to use poll in order to not rewrite a bunch of code for kqueue, it is built in to the python compiled from macports (macports.org). You just must specify that python instance explicitly (/opt/local/bin/python2.7 in my case) because OSX's python (/usr/bin/python) will by default be earlier in the search path.

Interestingly for future reference, this only comes up with a limited subset of the versions of python
user#hostname:~/ws/engine$ python
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import select
>>> select.poll()
<select.poll object at 0x102415cc0>
>>> exit()
user#hostname:~/ws/engine$ python --version
Python 2.7.9
user#hostname:~/ws/engine$ workon py_2_7_10
(py_2_7_10) user#hostname:~/ws/engine$ 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 select
>>> select.poll()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'poll'
>>>
~/ws/engine$ uname -a
Darwin hostname 15.4.0 Darwin Kernel Version 15.4.0: Fri Feb 26 22:08:05 PST 2016; root:xnu-3248.40.184~3/RELEASE_X86_64 x86_64

Related

tomcat user python module not found

tomcat user or for that matter any user using python2 or python3 is unable to find boto3 module, whereas in running python2 or 3 from root works fine.
-bash-4.2$ python
Python 2.7.5 (default, Nov 16 2020, 22:23:17)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named boto3
>>>
-bash-4.2$ whoami
tomcat
root user works
[root#server-name]# python
Python 2.7.5 (default, Nov 16 2020, 22:23:17)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto3
>>>

Twilio only runs from my Python 3.7 directory on OS X

I installed Twilio:
pip3 install twilio
But it will run only from the Python 3 Applications folder.
// ♥ python3
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 03:13:28)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more
information.
>>> import twilio
>>> print(twilio.__version__)
6.21.0
Here is what I get from any other folder:
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 03:13:28)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import twilio
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/davidtannenbaum/Dropbox (Personal)/pythonpractice/twilio.py", line 2, in <module>
print(twilio.__version__)
AttributeError: module 'twilio' has no attribute '__version__'
Anyone know why it works in the Python 3.7 directory and gives an error anywhere else? How do I fix this?

errors after upgrading to python3.4 on mac

After completing upgrading Python 2.7 to 3.4 on mac (10.10.3), I can't compile my codes.
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "/Users/tpmac/preBS.py", line 31, in <module>
doc = file(os.path.join(subdir,f)).read()
NameError: name 'file' is not defined
>>>
these codes are working with python 2.7 on my system.
There is no builtin file in python3, it has been removed just use open:
open(os.path.join(subdir,f)).read()
It would also be better use with when opening a file:
with open(os.path.join(subdir,f)) as fle:
doc = fle.read()
There is a comprehensive guide here on porting code from python2 to 3

python-whois , difference between python 2.7.6 and 2.7

I have bellow function running on Python 2.7.6 and python 2.7.5
This is 2.7.6
[root#NMCT-MSL-116-1 python_whois-0.2-py2.6.egg-info]# python
Python 2.7.6 (default, Sep 26 2014, 13:25:30)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import whois as wh
>>> wh.whois('199.96.57.7').emails
['mathew#twitter.com', 'sleibrand#twitter.com', 'noc#twitter.com', 'sconnor#twitter.com',
'rjohnson#twitter.com', 'fawad#twitter.com', 'net-abuse#twitter.com']
This is 2.7.5
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import whois as wh
>>> wh.whois('199.96.57.7').emails
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/whois/__init__.py", line 11, in whois
domain = extract_domain(url)
File "/usr/lib/python2.7/site-packages/whois/__init__.py", line 35, in extract_domain
return socket.gethostbyaddr(url)[0]
socket.herror: [Errno 1] Unknown host
It is same code 2 different Centos versions. First one is 6.5, custom python installation of 2.7.6. Second one is Centos 7 and 2.7.5 default installation. When i manually check both servers can find this host with whois cli tool.
I got stuck and I could not find what is the difference between both machines. They are based on same Network subnet, using same network config except IP addresses.
Does this a difference between versions ? Does anybody has an idea about this ?

PyObjC Not Working

I've installed PyObjC on my Mac, but I can't get it to work at all. Even trying to import the AddressBook class just fails immediately.
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import AddressBook
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "AddressBook.py", line 17, in <module>
('Last Name', AddressBook.kABLastNameProperty),
AttributeError: 'module' object has no attribute 'kABLastNameProperty'
Does anyone have any ideas as to how to solve this issue? Thanks.
You appear to have a conflicting file named AddressBook.py in your working directory. Rename or remove it or change to another directory.

Categories