Server Error:
python 2.7
Traceback (most recent call last):
File "promoter.py", line 325, in process_unfollowbacks
self.add_user_to_database(user_info)
File "promoter.py", line 618, in add_user_to_database
str(u['name']),
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 11: ordinal not in range(128)
However, it runs on Ubuntu fine.
Server:
Python 2.7.3 (default, Mar 2 2014, 10:09:03)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Ubuntu:
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
I'm getting this error by operating on a dictionary resulting from a Twitter Json array (Twython module) however I don't think the fault is there.
A little inspection shows this is happening with foreign languages.
The Question:
How do I deal with this inconsistency between environments?
The default encoding in your 'Server' environment is not the same as the Ubuntu environment. Try u['name'].encode('utf-8')
Related
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
>>>
I have been trying on setup PIL module on my Ubuntu System and it has been giving a lots of error. Finally , i am getting issue as:
when i try to run python from /home/ubuntu, it gives me error as:
ubuntu#ip-<server>:~$ python
Python 2.7.12 (default, Apr 15 2020, 17:07:12)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "PIL/Image.py", line 90, in <module>
from . import _imaging as core
ImportError: cannot import name _imaging
but if I run the code from /usr/local, it does not return any error:
ubuntu#ip-<server>:~$ cd /usr/local/
ubuntu#ip-<server>:/usr/local$ python
Python 2.7.12 (default, Apr 15 2020, 17:07:12)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>>
there might be an issue with the path or the permission of the required libraries. But i am not sure how to fix this.
I am executing my script from Jenkins and it throws error over there. Please guide how to fix.
I am having trouble with encoding in python 3.
When I was testing on my PC I get no errors:
Python 3.7.3 (default, Jun 24 2019, 04:54:02)
[GCC 9.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> print(requests.get('https://www.kinopoisk.ru').text)
everything good.
But when I ran this code on my VPS a have following error:
Python 3.7.3 (default, Apr 3 2019, 19:16:38)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> print(requests.get('https://www.kinopoisk.ru').text)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 393-401: ordinal not in range(256)
The python versions are the same. I don't know what is going on.
How do I fix it?
If your environment uses the C os POSIX locale, Python 3.7 automatically coerces that to a UTF-8 aware locale, according to pep-538.
So it seems that your PC has an UTF-8 or C locale set, while your VPS uses latin-1.
Try running the following in an interactive Python session on both machines:
import sys
import locale
print(sys.getfilesystemencoding())
print(locale.getpreferredencoding())
It you do not want to change the locale on your VPS, you could set PYTHONUTF8=1 in its environment, or you could use the -X utf-8 option with Python.
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 ?
$ 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