Pip install raises UnicodeDecodeError on Windows. Fix? - python

When trying to install mysql-python on my Windows 10 machine i get the following error:
File "<string>", line 1, in <module>
File "C:\Users\LUCAFL~1\AppData\Local\Temp\pip-build-3u7aih0l\mysql-python\setup.py", line 21, in <module>
setuptools.setup(**metadata)
File "c:\program files (x86)\python35-32\lib\distutils\core.py", line 148, in setup
dist.run_commands()
...
File "c:\program files (x86)\python35-32\lib\subprocess.py", line 1055, in communicate
stdout = self.stdout.read()
File "c:\program files (x86)\python35-32\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 1716: character maps to <undefined>
I tried installing other packages and received the same error on almost every one (one exception being pymysql). All of these packages were big and had dependencies. I guess that the big ones create temporary data in my user directories APPDATA folder. As you can see, the ü is not properly decoded (ü being byte 0x81). It's always a german umlaut that produces the error (mainly ü, as it's part of my user folders name).
I googled for the last 2 hours and found a lot of people having the same problem, but mostly they were opening github tickets or discussing the problem for Ubuntu/Fedora/OSX, etc. A couple times i read, that the standard encoding under windows is cp-1252 which causes the problem. Can i somehow force windows using my console to use utf-8 for this session and then run pip with that?
Please don't recommend me renaming my user folder. It's not easily done under Windows 10 and i dont want to re-install windows just because of python.
My setup: Windows 10, Python 3.5.1, pip 8.0.3

Can you try the following and see if it works. Replace path for python by your actual path.
I am not able to simulate on my windows laptop.
import sys
import subprocess
reload(sys) # Reload may do the trick!
sys.setdefaultencoding('UTF8')
theproc =subprocess.call(['C:\\Python27\\Scripts\\pip.exe', 'install', 'mysql-python'])
theproc.communicate()

Related

Google Cloud SDK install failed UnicodeDecodeError: 'ascii' codec

I got the following error when installing the Google Cloud SDK on my Windows 10 computer:
ERROR: gcloud failed to load: 'ascii' codec can't decode byte 0xe1 in position 13: ordinal not in range(128)
gcloud_main = _import_gcloud_main()
import googlecloudsdk.gcloud_main
from googlecloudsdk.calliope import cli
from googlecloudsdk.calliope import backend
from googlecloudsdk.calliope import parser_extensions
from googlecloudsdk.core.updater import update_manager
from googlecloudsdk.core.updater import installers
from googlecloudsdk.core.credentials import store
from googlecloudsdk.core.credentials import creds
from googlecloudsdk.core.credentials import devshell as c_devshell
from oauth2client import client
from oauth2client import crypt
from oauth2client import _openssl_crypt
from OpenSSL import crypto
from OpenSSL import crypto, SSL
from cryptography import x509
from cryptography.x509.base import (
from cryptography.x509.extensions import Extension, ExtensionType
from asn1crypto.keys import PublicKeyInfo
from ._elliptic_curve import (
from ._int import inverse_mod
from ._perf._big_num_ctypes import libcrypto
libcrypto_path = find_library('crypto')
fname = os.path.join(directory, name)
result_path = result_path + p_path
This usually indicates corruption in your gcloud installation or problems with your Python interpreter.
Please verify that the following is the path to a working Python 2.7 executable:
C:\AndroidSdk\CloudSdk\google-cloud-sdk\platform\bundledpython\python.exe
If it is not, please set the CLOUDSDK_PYTHON environment variable to point to a working Python 2.7 executable.
If you are still experiencing problems, please reinstall the Cloud SDK using the instructions here:
https://cloud.google.com/sdk/
Traceback (most recent call last):
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\\bin\bootstrapping\install.py", line 12, in <module>
import bootstrapping
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\bin\bootstrapping\bootstrapping.py", line 44, in <module>
from googlecloudsdk.core.credentials import store as c_store
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\lib\googlecloudsdk\core\credentials\store.py", line 35, in <module>
from googlecloudsdk.core.credentials import creds
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\lib\googlecloudsdk\core\credentials\creds.py", line 34, in <module>
from googlecloudsdk.core.credentials import devshell as c_devshell
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\lib\googlecloudsdk\core\credentials\devshell.py", line 30, in <module>
from oauth2client import client
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\lib\third_party\oauth2client\client.py", line 51, in <module>
from oauth2client import crypt
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\lib\third_party\oauth2client\crypt.py", line 45, in <module>
from oauth2client import _openssl_crypt
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\lib\third_party\oauth2client\_openssl_crypt.py", line 16, in <module>
from OpenSSL import crypto
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\platform\bundledpython\lib\site-packages\OpenSSL\__init__.py", line 8, in <module>
from OpenSSL import crypto, SSL
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\platform\bundledpython\lib\site-packages\OpenSSL\crypto.py", line 12, in <module>
from cryptography import x509
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\platform\bundledpython\lib\site-packages\cryptography\x509\__init__.py", line 9, in <module>
from cryptography.x509.base import (
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\platform\bundledpython\lib\site-packages\cryptography\x509\base.py", line 16, in <module>
from cryptography.x509.extensions import Extension, ExtensionType
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\platform\bundledpython\lib\site-packages\cryptography\x509\extensions.py", line 13, in <module>
from asn1crypto.keys import PublicKeyInfo
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\platform\bundledpython\lib\site-packages\asn1crypto\keys.py", line 22, in <module>
from ._elliptic_curve import (
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\platform\bundledpython\lib\site-packages\asn1crypto\_elliptic_curve.py", line 51, in <module>
from ._int import inverse_mod
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\platform\bundledpython\lib\site-packages\asn1crypto\_int.py", line 56, in <module>
from ._perf._big_num_ctypes import libcrypto
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\platform\bundledpython\lib\site-packages\asn1crypto\_perf\_big_num_ctypes.py", line 31, in <module>
libcrypto_path = find_library('crypto')
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\platform\bundledpython\lib\ctypes\util.py", line 53, in find_library
fname = os.path.join(directory, name)
File "C:\AndroidSdk\CloudSdk\google-cloud-sdk\platform\bundledpython\lib\ntpath.py", line 85, in join
result_path = result_path + p_path
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe1 in position 13: ordinal not in range(128)
Failed to install.
I have tried redownloading the installer, I run it with admin rights and I have choosen an install folder without any non-ascii characters or spaces.
I also verified that the installer set up the python successfully, more specifically I have Python version 2.7.13 at C:\AndroidSdk\CloudSdk\google-cloud-sdk\platform\bundledpython\python.exe
I also have tried to set the CLOUDSDK_PYTHON variable without success.
Note: I saw this question, but I have a different version (213.0.0) of the cloud-sdk and I didn't find the mentioned part in my code, so I don't think this is a duplicate question.
EDIT 1:
My ntpath.py line 63-90:
def join(path, *paths):
"""Join two or more pathname components, inserting "\\" as needed."""
result_drive, result_path = splitdrive(path)
for p in paths:
p_drive, p_path = splitdrive(p)
if p_path and p_path[0] in '\\/':
# Second path is absolute
if p_drive or not result_drive:
result_drive = p_drive
result_path = p_path
continue
elif p_drive and p_drive != result_drive:
if p_drive.lower() != result_drive.lower():
# Different drives => ignore the first path entirely
result_drive = p_drive
result_path = p_path
continue
# Same drive in different case
result_drive = p_drive
# Second path is relative to the first
if result_path and result_path[-1] not in '\\/':
result_path = result_path + '\\'
result_path = result_path + p_path #85
## add separator between UNC and non-absolute path
if (result_path and result_path[0] not in '\\/' and
result_drive and result_drive[-1:] != ':'):
return result_drive + sep + result_path
return result_drive + result_path
EDIT 2:
Thank you for your suggestions I have checked these SO questions and answers also, but I could not find a solution:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 13: ordinal not in range(128)
Bitbucket Pipelines: gcloud crashed (UnicodeDecodeError)
Note that I do not have any non-ascii characters in gcloud path, I have checked it with the following command: gci -recurse . | where {$_.Name -match "[^\u0000-\u007F]"}
Any other ideas how to fix?
Just a list of things to try:
install Python from https://www.python.org/downloads/release/python-2715/. Remember the location of the python binary. Set that as CLOUDSDK_PYTHON
Show the printout with the variable set.
Ensure that the variable is really set.
In C:\ (or C:\AndroidSdk, but optimally C:\): Run the gci command in https://superuser.com/questions/237533/find-files-with-non-ascii-characters-in-filenames-in-windows-xp, show the output. Or run the python program from that same question.
would be found by 4., but the 13th character seems to be the \. Try to re-create the directory, giving the AndroidSDK a shorter/longer name, see if it still is the 13th character
I just had the same problem and it took me a while to solve it, so here's my findings for posterity:
I had the npm package windows-build-tools installed (using npm install --global windows-build-tools), and that package apparently came with a separate Python installation in C:\Users\[my user]\.windows-build-tools which I had forgotten about. That folder was also in my PATH.
Now unfortunately my Windows user name contains non-ASCII characters. As far as I'm aware, there is no way for me to change this, as my user is tied to a MS account that my employer manages.
Despite me installing the SDK in a unproblematic location (D:\CloudSDK) and setting CLOUDSDK_PYTHON to another Python 2.7.15 installation, I kept getting the error described in the question, until I removed the windows build tools python installation plus every other trace of any previous Python installation I found and re-installed 2.7.15 cleanly. I then retried the Cloud SDK installation without the bundled Python and used my clean new one from PATH, which solved the problem.
Maybe this helps someone.
Same problem with non-ascii chars. I edited the cloud_env.bat in the install root folder (mine is C:\prg\GCP) and REMOVED appending the %PATH% (see below) - problem solved for install.
I also had to add this manual to the PATH after install:
C:\prg\GCP\google-cloud-sdk\bin;C:\prg\GCP\google-cloud-sdk\platform\bundledpython\;
AND update the USERPROFILE=C:\prg\GCP (mine contained non-ascii).
cloud_env.bat
ECHO OFF
CLS
REM SET PATH=C:\prg\GCP\google-cloud-sdk\bin;%PATH%;
SET PATH=C:\prg\GCP\google-cloud-sdk\bin;
cd C:\prg\GCP
ECHO Welcome to the Google Cloud SDK! Run "gcloud -h" to get the list of available commands.
ECHO ---
ECHO ON`
For me, window10,
it happen because the python 2.7 paths get found before the python 3.7,
I simply delete 2.7 PATH
and SET CLOUDSDK_PYTHON point to the 3.7 paths and it works.

BeautifulSoup code works in IPython Notebook but not Eclipse

The following code works fine when run from Jupyter IPython notebook:
from bs4 import BeautifulSoup
xml_file_path = "<Path to XML file>"
s = BeautifulSoup(open(xml_file_path), "xml")
But it fails when creating the soup when run from Eclipse/PyDev (which uses the same Python interpreter):
Traceback (most recent call last):
File "~/parser/scratch.py", line 3, in <module>
s = BeautifulSoup(open(xml_file), "xml")
File "/anaconda/lib/python3.5/site-packages/bs4/__init__.py", line 175, in __init__
markup = markup.read()
File "/anaconda/lib/python3.5/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 1812: ordinal not in range(128)
Python version: 3.5.2 (Anaconda 4.1.1)
BeautifulSoup: version 4
IPython Notebook version: 4.2.1
Eclipse version: Mars.2 Release (4.5.2)
PyDev version: 5.1.2.20160623256
Mac OS X: El Capitan 10.11.6
UPDATE:
The character in the file that is causing issue in Eclipse is �, but this causes no issues in IPython Notebook! If I remove this character from the XML file, then the code works fine in Eclipse as well. Is there some setting in Eclipse I need to change so that the code won't fail on this (and possibly other such) character?
I think that you have to open with open(xml_file_path, 'rb') -- and specify the encoding for things to work the same in both (otherwise you're having an implicit conversion from bytes to unicode -- and apparently it uses a different encoding based on your env, since you have something in Eclipse and another thing in IPython).
Try doing:
with open(xml_file_path, 'rb') as stream:
contents = stream.read()
contents.decode('utf-8')
Just to check if you're really able to decode it as utf-8 (i.e.: to check if that char is a valid utf-8 char).

Opening an UTF-8 encoded file by a Python 3.6 script running in PyCharm 2016.3.2

I have a quite odd problem with PyCharm and a Python app that I am working on.
Pycharm is PyCharm Community Edition 2016.3.2
The project interpreter is: 3.6.0
OS is MacOS Sierra
As I am have been googling for a solution for some time and no proposed idea helps I want to ask here.
I want to open an UTF-8 encoded file using the following code:
#!/usr/bin/env python3
import os, platform
def read(file):
f = open(file, "r")
content = f.read()
f.close()
return content
print(platform.python_version())
print(os.environ["PYTHONIOENCODING"])
content = read("testfile")
print(content)
The code crashes when run in PyCharm. The output is
3.6.0
UTF-8
Traceback (most recent call last):
File "/Users/xxx/Documents/Scripts/pycharmutf8/file.py", line 14, in <module>
content = read("testfile")
File "/Users/xxx/Documents/Scripts/pycharmutf8/file.py", line 7, in read
content = f.read()
File "/usr/local/Cellar/python3/3.6.0_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128)
When I run the identical code from command line, it works just fine:
./file.py
3.6.0
utf-8:surrogateescape
I am a file with evil unicode characters: äöü
I have found out that in comparable situations people are advised to set the environment variable PYTHONIOENCODING to utf-8:surrogateescape that I did (as you can see in above output) system-wide
export PYTHONIOENCODING=utf-8:surrogateescape
but also in PyCharm itself (Settings -> Build -> Console -> Python Console -> Environment variables).
This does not have any effect. Do you have further suggestions?
If it's harder to change the encoding for the open call i.e. it's happening in a library you can change this environment variable in the run configurations: LC_CTYPE=en_US.UTF-8
Source:
PyCharm is changing the default encoding in my Django app
If you want to read a UTF8 file, specify the encoding:
def read(file):
with open(file, encoding='utf8') as f:
content = f.read()

AWS UnicodeDecodeError when creating application on ElasticBeanstalk

Trying to create a Python application on ElasticBeanstalk using the CLI, after I select my platform version I get the following error:
Select a platform version.
1) Python 3.4
2) Python
3) Python 2.7
4) Python 3.4 (Preconfigured - Docker)
(default is 1): 1
ERROR: UnicodeDecodeError :: 'ascii' codec can't decode byte 0xe2 in position 891: ordinal not in range(128)
I found this: https://github.com/aws/aws-cli/issues/708 which makes me think maybe I pasted my AWS credentials with a newline attached. I'd like to re-enter the credentials but it seems they are cached somewhere (it asked me for them on the first install attempt but not any subsequence attempts). I do not see them in my environment vars and I have been deleting .elasticbeanstalk between attempts. Any ideas?
update: I deleted .aws/config so it allowed me to re-enter my credentials. Still get the same error when I try to run eb init
update2: gist of --debug output can be found here: https://gist.github.com/dshuhler/8d64849021c48bf1ba71
tldr; My .gitignore had a hidden non-ascii character as I had copy pasted a Python specific .gitignore from a popular Github repository.
Steps I took to figure out the root cause
So, I couldn't find any answer that helped me solve this problem for me. Looks like, different people are seeing this error for different reasons.
I'm going to share the steps I took to figure out the cause for my problem. Make sure to run your command using --debug flag
$ eb init --debug
My output was something like this
2019-05-05 13:44:17,548 (INFO) eb : Traceback (most recent call last):
File "/Users/mudassirali/.virtualenvs/rtp_dev/lib/python3.6/site-packages/ebcli/core/ebrun.py", line 62, in run_app
app.run()
File "/Users/mudassirali/.virtualenvs/rtp_dev/lib/python3.6/site-packages/cement/core/foundation.py", line 797, in run
return_val = self.controller._dispatch()
File "/Users/mudassirali/.virtualenvs/rtp_dev/lib/python3.6/site-packages/cement/core/controller.py", line 472, in _dispatch
return func()
File "/Users/mudassirali/.virtualenvs/rtp_dev/lib/python3.6/site-packages/cement/core/controller.py", line 478, in _dispatch
return func()
File "/Users/mudassirali/.virtualenvs/rtp_dev/lib/python3.6/site-packages/ebcli/core/abstractcontroller.py", line 89, in default
self.do_command()
File "/Users/mudassirali/.virtualenvs/rtp_dev/lib/python3.6/site-packages/ebcli/controllers/initialize.py", line 118, in do_command
initializeops.setup(app_name, region_name, platform, dir_path=None, repository=repository, branch=branch)
File "/Users/mudassirali/.virtualenvs/rtp_dev/lib/python3.6/site-packages/ebcli/operations/initializeops.py", line 57, in setup
setup_ignore_file()
File "/Users/mudassirali/.virtualenvs/rtp_dev/lib/python3.6/site-packages/ebcli/operations/initializeops.py", line 92, in setup_ignore_file
source_control.set_up_ignore_file()
File "/Users/mudassirali/.virtualenvs/rtp_dev/lib/python3.6/site-packages/ebcli/objects/sourcecontrol.py", line 294, in set_up_ignore_file
for line in f:
File "/Users/mudassirali/.virtualenvs/rtp_dev/lib/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 1287: ordinal not in range(128)
I jumped into the source code, specifically this bit
for line in f:
I found out this is the part which was throwing the error
with open('.gitignore', 'r') as f:
for line in f:
if line.strip() == git_ignore[0]:
return
Once I removed the .gitignore it worked like charm(and added it later without copy-paste)
It could be that your Beanstalk runs the Python environment without LC_ALL defined. Setting LC_ALL=en_US.UTF-8 in my Beanstalk environment properties resolved this issue for me.
Some background on these locale environment variables is available at: Explain the effects of export LANG, LC_CTYPE, LC_ALL
your aws credentials are at ~/.aws/config

Ropemacs does not work under windows environment

I am trying to use Emacs as my Python IDE under win32. I referred to several posts on the Internet and I had yasinppet, auto-complete, pymacs, ropemacs installed on my Emacs. It shows that Emacs will load python mode, rope mode, yas mode and auto-complete mode after I create a *.py file. But I got an error saying that the system cannot find pymacs.py file when I tried to use functions like "Goto definition" (C-c g) of rope. I am quite sure I have installed pymacs correctly and the path is right. So anyone can help me out of this?
WindowsError: [Error 3] The system cannot find the path specified:
Traceback (most recent call last):
File "C:\\Python27\\Pymacs\\Pymacs\\pymacs.py", line 265, in loop
value = eval(text)
File "<string>", line 1, in <module>
UnicodeDecodeError: \'ascii\' codec can\'t decode byte 0xe5 in position 12:
ordinal not in range(128)

Categories