numpy.load unable to interpret pickle file as pickle in anaconda environment - python

I have two python scripts where one needs to be run using the older version of python (2.7) and other in the newer version i.e. 3.6. So I created two environments in Anaconda- one with python 2.7.8 (old) and the other with Python 3.6 (new). I am using a data files say “test_input.csv” and “test_input.sig”.
I am able to run the first script file in the python 2.7.8 environment which basically preprocess the data i.e. unwrapping categorical variables, normalizing the variables, etc and dumps it into a pickle. So, it generated the “test_input.csv_normalized.pickle” file. But now when I am trying to run the second script in python 3.6 in Anaconda where I am trying to load this pickle file i.e.
import numpy as np
y=np.load('test_input.csv_normalized.pickle')
But here I am getting the following error:-
File "/..../python3.6/site-packages/numpy/lib/npyio.py", line 413, in
load return pickle.load(fid, **pickle_kwargs)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xea in position
0: ordinal not in range(128) During handling of the above exception,
another exception occurred:
Traceback (most recent call last): File
"ClusterId.py", line 84, in
y=np.load('test_input.csv_normalized.pickle') File
"/home/putatund/anaconda3/lib/python3.6/site-packages/numpy/lib/npyio.py",
line 416, in load "Failed to interpret file %s as a pickle" %
repr(file))
OSError: Failed to interpret file
'test_input.csv_normalized.pickle' as a pickle
Any idea what might be the issue? And how to resolve this.

Related

UnicodeDecodeError when trying to open Idle for Python 3.8

I tried launching idle on the command prompt by using python -i -m idlelib
This the error that occurred:
Fatal Python error: init_import_size: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
File "C:\Users\_\AppData\Local\Programs\Python\Python38\lib\site.py", line 580, in <module>
main()
File "C:\Users\_\AppData\Local\Programs\Python\Python38\lib\site.py", line 567, in main
known_paths = addsitepackages(known_paths)
File "C:\Users\_\AppData\Local\Programs\Python\Python38\lib\site.py", line 350, in addsitepackages
addsitedir(sitedir, known_paths)
File "C:\Users\_\AppData\Local\Programs\Python\Python38\lib\site.py", line 208, in addsitedir
addpackage(sitedir, name, known_paths)
File "C:\Users\_\AppData\Local\Programs\Python\Python38\lib\site.py", line 164, in addpackage
for n, line in enumerate(f):
File "C:\Users\_\AppData\Local\Programs\Python\Python38\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 0x90 in position 654: character maps to undefined
Try python -i first and see if you can access the idle on the console.
Once you've verified that, you should then open idle on your pc and try import idlelib. If it doesn't load that means that either you don't have that module or its not in you path (which you can fix by finding its location and running import sys; sys.path.append("path/to/module")).
My guess would be that you have multiple versions of python on your pc and when you're running python you are not using the version that has this module.
A person here seems to have had a similar problem, albeit with a different program where the environmental variables they used caused problems. I recommend getting a package like conda. That'll reduce the likelihood of running into these problems immensely.

Trouble using Javabridge with Python

I successfully installed javabridge using $pip3 install javabridge on my Mac's virtual environment. But when I tried to import javabridge in my python3 code, this is what I get:
import javabridge
Failed to run /usr/libexec/java_home, defaulting to best guess for Java
Traceback (most recent call last):
File "/Users/puifai/Documents/data_science/venv/venv3/lib/python3.6/site-packages/javabridge/locate.py", line 45, in find_javahome
os.path.join(os.path.dirname(path), "Libraries"),
File "/Users/puifai/Documents/data_science/venv/venv3/bin/../lib/python3.6/posixpath.py", line 92, in join
genericpath._check_arg_types('join', a, *p)
File "/Users/puifai/Documents/data_science/venv/venv3/bin/../lib/python3.6/genericpath.py", line 151, in _check_arg_types
raise TypeError("Can't mix strings and bytes in path components") from None
TypeError: Can't mix strings and bytes in path components
Any ideas on how to fix this? It looks like a problem with Python rather than javabridge?
Pardon the less-than-complete answer, but it has to do with the difference in strings between Python2 and Python3. Working in Python2.7 will at least get you around the problem for the moment. See this post for more background.

Pip install raises UnicodeDecodeError on Windows. Fix?

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()

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