Load pickle file(comes from python3) in python2 - python-2.7

I have a pickle file, with
>>> with open("wikilinks.pickle", "rb") as f:
... titles, links = pickle.load(f)
...
>>> len(titles)
13421
I can load it in python3.
However, when I try to load it in python2, I get this message:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib/python2.7/pickle.py", line 1378, in load
return Unpickler(file).load()
File "/usr/lib/python2.7/pickle.py", line 858, in load
dispatch[key](self)
File "/usr/lib/python2.7/pickle.py", line 886, in load_proto
raise ValueError, "unsupported pickle protocol: %d" % proto
ValueError: unsupported pickle protocol: 3
So how to load it in python2?

Read in python3 and save it as python2 format
#!/usr/bin/env python3
import pickle
with open("a.pkl", "rb") as f:
w = pickle.load(f)
pickle.dump(w, open("a_py2.pkl","wb"), protocol=2)
Then you can load a_py2.pkl in python2.

The default pickling protocol used by default in Python 3 is incompatible with the protocol used by Python 2. However, if you can modify the code that produces wikilink.pickle, you can tell it to use one of the earlier protocols (0, 1, or 2) which Python 2 will understand just fine.
Be warned, though, that Python 3 strings will come up as Unicode strings in Python 2, which may be surprising in some use cases.
$ python3
Python 3.4.1 (default, May 19 2014, 17:23:49)
[GCC 4.9.0 20140507 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> pickle.dumps([1, 2, 'abc'], 2)
b'\x80\x02]q\x00(K\x01K\x02X\x03\x00\x00\x00abcq\x01e.'
>>>
$ python
Python 2.7.8 (default, Jul 1 2014, 17:30:21)
[GCC 4.9.0 20140604 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cPickle
>>> cPickle.loads('\x80\x02]q\x00(K\x01K\x02X\x03\x00\x00\x00abcq\x01e.')
[1, 2, u'abc']

Related

Cannot import socketio (python-socketio) in Python2.7

Getting this error when trying to import socketio in python 2:
root#udacity:~/Desktop/CarND-Capstone-master/ros# python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socketio
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/socketio/__init__.py", line 3, in <module>
from .client import Client
File "/usr/local/lib/python2.7/dist-packages/socketio/client.py", line 6, in <module>
import engineio
File "/usr/local/lib/python2.7/dist-packages/engineio/__init__.py", line 3, in <module>
from .client import Client
File "/usr/local/lib/python2.7/dist-packages/engineio/client.py", line 302
'Unexpected response from server') from None
^
SyntaxError: invalid syntax
Tried reinstalling/installing socketio and engineio, but can't seem to get it to work. I should mention that I'm running in an Ubuntu VM.
I found the answer, as shown above, it was an engineio fault. Installing it before socketio in my requirements file worked. Particularly I'm using python-socketio==4.4.0, and python-engineio==3.11.2
python-engineio==3.11.2
python-socketio==4.4.0
Reference:
https://github.com/udacity/CarND-Capstone/issues/342

Using Django in Docker unable to fetch rest api

I have built the webapp using Django and also configured for Rest api. I am able to fetch the data via curl and also by python script. Issue I'm running into, when using Docker container, when user fill in the data and submit, it goes thru and ran the python function, but it stop working at when fetching the data via api and thru errors
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/site-packages/requests/models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib64/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I'm able to curl the URL inside the container. I get the same error when shell into the container in python3.6
sh-4.2# python3.6
Python 3.6.8 (default, May 2 2019, 20:40:44)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import requests, json
>>> url = 'http://127.0.0.1:8001/db/data/'
>>> response = requests.get(url)
>>> parsedjson = json.loads(response.text)
ERROR SAME AS ABOVE . . .
parsedjson = response.json()
ERROR SAME AS ABOVE . . .
>>> print(response.status_code)
503
>>> exit()
sh-4.2# curl http://127.0.0.1:8001/db/data/
[{"center":{"name":"ABC"},"main":{"code":{"name":"ABC"},"subnet":"10.10.12.0",...."}]
sh-4.2#
any idea? how to fix it?
thank you for the help in advance..

Error importing tensorflow running python in the home directory

Team,
I have a problem importing tensorflow if I run python from the home directory terminal. Please see below:
rkenaya#rkenaya:~$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/tensorflow/__init__.py", line 24, in <module>
from tensorflow.python import *
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/__init__.py", line 52, in <module>
from tensorflow.core.framework.graph_pb2 import *
File "/usr/local/lib/python2.7/dist-packages/tensorflow/core/framework/graph_pb2.py", line 16, in <module>
from tensorflow.core.framework import node_def_pb2 as tensorflow_dot_core_dot_framework_dot_node__def__pb2
File "/usr/local/lib/python2.7/dist-packages/tensorflow/core/framework/node_def_pb2.py", line 16, in <module>
from tensorflow.core.framework import attr_value_pb2 as tensorflow_dot_core_dot_framework_dot_attr__value__pb2
File "/usr/local/lib/python2.7/dist-packages/tensorflow/core/framework/attr_value_pb2.py", line 16, in <module>
from tensorflow.core.framework import tensor_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__pb2
File "/usr/local/lib/python2.7/dist-packages/tensorflow/core/framework/tensor_pb2.py", line 16, in <module>
from tensorflow.core.framework import resource_handle_pb2 as tensorflow_dot_core_dot_framework_dot_resource__handle__pb2
File "/usr/local/lib/python2.7/dist-packages/tensorflow/core/framework/resource_handle_pb2.py", line 22, in <module>
serialized_pb=_b('\n/tensorflow/core/framework/resource_handle.proto\x12\ntensorflow\"r\n\x13ResourceHandleProto\x12\x0e\n\x06\x64\x65vice\x18\x01 \x01(\t\x12\x11\n\tcontainer\x18\x02 \x01(\t\x12\x0c\n\x04name\x18\x03 \x01(\t\x12\x11\n\thash_code\x18\x04 \x01(\x04\x12\x17\n\x0fmaybe_type_name\x18\x05 \x01(\tB/\n\x18org.tensorflow.frameworkB\x0eResourceHandleP\x01\xf8\x01\x01\x62\x06proto3')
TypeError: __init__() got an unexpected keyword argument 'syntax'
>>>
However I do not get any error if I run python from python native directory. Please see below:
rkenaya#rkenaya:/usr/local/lib/python2.7/dist-packages$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
>>>
Note: I am using ubuntu 16.04 LTS
Any help would be highly appreciated folks.
Riyadh

Invalid syntax in Python script of rnaQUAST

I have been trying to run a software, but since I am not an expert in python I do not know what this syntax error could be. I get the following error message:
Traceback (most recent call last):
File "rnaQUAST.py", line 29, in <module>
from metrics import TranscriptsMetrics
File "/NFS/home/ffalcon/TOOLS/rnaQUAST1.4.0/metrics/TranscriptsMetrics.py", line 6, in <module>
from general import UtilsCoverage
File "/NFS/home/ffalcon/TOOLS/rnaQUAST1.4.0/general/UtilsCoverage.py", line 234
covered_isoform_fraction_max = {id_isoform: isoforms_covered_fraction[id_isoform] for id_isoform in ids_isoforms_max}
^
SyntaxError: invalid syntax
dict comprehension works python2.7 upwards. Based on your comment, I believe that you got the error from python2.6 as below:
Python2.6
Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> mydict = {"a":1}
>>> mylist = ["a"]
>>> {x:mydict[x] for x in mylist}
File "<stdin>", line 1
{x:mydict[x] for x in mylist}
^
SyntaxError: invalid syntax
Python2.7
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.
>>> mydict ={"a":1}
>>> mylist=["a"]
>>> {x:mydict[x] for x in mylist}
{'a': 1}
Here's my solution:
>>> data = dict((x,mydict[x]) for x in mydict)
>>> data
{'a': 1}

Python 2.7 & wget error 17 "No usable temporary file name found"

I am just learning python 2.7 and trying to use wget to download a file from a URL.
I get the following error (and here are the exact input & syntax):
Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import wget
>>> wget.download('http://www.csun.edu/~rd436460/Labview/Lecture-Overview.pdf')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\wget.py", line 506, in download
(fd, tmpfile) = tempfile.mkstemp(".tmp", prefix=prefix, dir=".")
File "C:\Python27\lib\tempfile.py", line 314, in mkstemp
return _mkstemp_inner(dir, prefix, suffix, flags)
File "C:\Python27\lib\tempfile.py", line 257, in _mkstemp_inner
raise IOError, (_errno.EEXIST, "No usable temporary file name found")
IOError: [Errno 17] No usable temporary file name found
>>>
My question is....... What is the correct syntax to get this file to download without error?
I am running Python 2.7 On Windows 10...... Also I saw some of the other post of temporary file errors due to no drive space. On my system I have plenty of drive space available.