I have been used python and Abaqus for a long time. But when i upgraded my python from 2.7 to 3.5.2 some error occures. I try to pickle some object A of my class.
f = open(utilsDir + "aclass.log", 'wb')
pickle.dump(A,f,protocol=2)
f.close()
and then unpickle it with abaqus' python, which is still 2.7.
filepath = utilsDir + 'aclass.log'
A1 = pickle.load(file(filepath))
All it has worked before updating my python, but now i have an error:
This is old and the answer will not help the OP, but in case anyone stumbles on this for a code he can modify, this error usually appears when the class pickled in Python 2 is not a new style class, i.e. does not inherit from object.
Related
I am trying to work with the code below. However it is a written code by my professor and also in python2 (I already made quite some changes). I use python 3.8.5.
When I try to run the code it yield me the Attribute Error below. Trying to adapt it by changing it the syntax or shorten up to save did not work and I also do not came across any reference of save in the web.
def __init__(self, options):
self.save_each = options.save_each
self.origin_dir = "NET-%s-%s-%s"%(options.currency, options.heuristic, options.epoch)
if options.save_each is not None:
self.origin_dir += "_%s"%options.save_each
File "comp-1-network_centrality.py", line 31, in __init__
self.save_each = options.save_each
AttributeError: 'Values' object has no attribute 'save_each'
Looking forward to help.
I ran into an issue recently where I tried to do Pickle (Python 3.6) on an object of type:
twilio.rest.chat.v2.service.channel.ChannelInstance (The code for it can be found here. )
The relevant code was just this:
picked_rv = pickle.dumps(value)
it would result in: TypeError: can’t pickle _thread.RLock objects on our server
or
AttributeError: Can't pickle local object 'init_logging.<locals>.NotProductionHandler'
on my local system. Both RLock and NotProductionHandler are objects in the same environment as the ChannelInstance but not actually referenced by it at all, i.e. those are objects I instantiated in the same context while making a call to Twilio and getting the ChannelInstance back.
However, as soon as I try to use the pure Python Pickle instead of the cPickle (or _pickle in the module) dumps like this:
picked_rv = pickle._dumps(value)
it would work just fine. Can anyone explain the difference in behavior? I'm just baffled by both the error and difference in behavior. Why would cPickle try to pull in the other variables and objects in the context and then why would the pure Python implementation work?
This was not an issue with on Twilio 6.18.0, cPickle worked just fine, but I suspect the issue is Pickle.
Thanks in advance.
Suppose I have the following code excerpt:
import pickle
with open('my_object.pkl','r') as f:
object = pickle.load(f)
My question is:
Suppose object is from a class I defined previously, how can I specify this in the code such that my interpreter knows prior to running the code what the object class is ? My goal here is to have the auto-completion of my IDE (I use VSCode) recognize the object so I can auto-complete and easily search the methods and attributes of that object.
It depends on the version of Python and IDE, but in general looks like an additional statement with assertion instance type is the only way so far. This will trigger VS autocomplete settings
import pickle
with open('my_object.pkl','r') as f:
object = pickle.load(f)
assert isinstance(object, YourType)
# and now you can use autocompletion with the object
The following issue is tracking that feature: #82.
When I try to use gplot2.geom_dotplot I get the error:
AttributeError: 'module' object has no attribute 'geom_dotplot'
Does this function have a different name in Rpy2? thanks.
The mapping of that function is just missing. This is a bug with rpy2. The fix will in the repository shortly (will be released with version 2.3.4).
In the meantime a workaround can be to add the following to your code.:
from rpy2.robjects.lib import ggplot2
class GeomDotplot(ggplot2.Geom):
_constructor = ggplot2.ggplot2_env['geom_dotplot']
ggplot2.geom_dotplot = GeomDotplot.new
I have been running win32com to access AutoCAD for quite some time without issue. I learned that applying the makepy utility could create a more user friendly experience at the interactive prompt, so I ran it and added the "AutoCAD 2006 Type Library". Now, some common attributes that I used to access are no longer available. Consider the code:
acad = win32com.client("AutoCAD.Application")
doc = acad.Documents.Open('mydoc.dwg')
ms = doc.ModelSpace
count = ms.Count #counts all entities in dwg
for i in range(count):
item = ms.Item(i)
if 'block' in item.ObjectName.lower():
print item.Name
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 462, in
__getattr__ raise AttributeError("'%s' object has no attribute '%s'"
(repr(self),attr))
AttributeError: '<win32com.gen_py.AutoCAD 2006 Type Library.IAcadEntity instance
at 0x34894552>' object has no attribute 'Name'
Name has always been an accessible attribute of a block until I ran makepy. I've tried reinstalling Python for windows to see if that would reset it, but it didn't work.
Does makepy create another file upon implementation that I need to remove?
So many of my scripts depend upon the Name attribute. Any suggestions or help you could offer would be greatly appreciated.
The main reason for this attribute error is because your COM-server has shifted from late-binding (dynamic) to early binding (static).
In Late Binding, whenever a method is called, the object is queried for the method and if it succeeds, then the call can be made.
In Early Binding, the information of the object model is determined in advance from type information supplied by the object call. Early binding makes use of MakePy. Also, early binding is case sensitive.
There are two ways to fix this issue:
Use the dynamic module to force your code to work in a late-bound oriented way. Example use:
win32com.client.dynamic.Dispatch() instead of win32com.client.Dispatch()
Use camelcase sensitive keywords for the early bound oriented way. Example use:
excel.Visible() instead of excel.VISIBLE() or excel.visible()
So try using dynamic.Dispatch or case-sensitive variable names.
I encounter a similar problem when I run win32com to access DELMIA. I find out that delmia has lots of com stuff as .tlb files. When I type:
from win32com.client.gencache import EnsureDispatch
EnsureDispatch('DELMIA.Application')
Python will automatically generate some stuff in win32com\gen_py directory, same as example of Word or Excel. But, the difference is that the generated stuff is from only one .tlb file of delmia. If I access some variable in the `.tlb, it's ok:
docs = delmia.Documents
If I access some variable in other .tlb, I get a similar error:
pdoc = docs.Add('Process')
Python says that:
... object has no attribute ...
So I delete all files in the win32com\gen_py directory but keep gen_py directory, and it is ok now.