AttributeError: module 'dataclasses' has no attribute 'is_dataclass' - python-3.x

I am trying to create a dynamic model using pydantic but it seems it can't get even the basic example:
from pydantic import BaseModel, create_model
MyModel = create_model('MyModel', foo="foo")
The error is:
MyModel = create_model('MyModel', foo="foo")
File "pydantic/main.py", line 972, in pydantic.main.create_model
File "pydantic/main.py", line 228, in pydantic.main.ModelMetaclass.__new__
File "pydantic/fields.py", line 488, in pydantic.fields.ModelField.infer
File "pydantic/fields.py", line 419, in pydantic.fields.ModelField.__init__
File "pydantic/fields.py", line 539, in pydantic.fields.ModelField.prepare
File "pydantic/fields.py", line 801, in pydantic.fields.ModelField.populate_validators
File "pydantic/validators.py", line 682, in find_validators
File "pydantic/dataclasses.py", line 82, in pydantic.dataclasses.is_builtin_dataclass
AttributeError: module 'dataclasses' has no attribute 'is_dataclass'
Since I am using poetry, the command I used is:
poetry run python main.py
Anyone has any idea why it gets such an error?

THe reason is because I have another file dataclasses.py in the same folder as main.py and poetry run python main.py used my local datacalsses.py automatically and thus override the system dataclasses somehow. A good lesson here is do not use class names as your local module name.

Related

joblib: importing .pkl file with personal classes

I'm using Jupyter to learn and practice machine learning. I created a Pipeline object with many classes from Scikit Learn and custom classes that I wrote. After that I saved this Pipeline object in a file 'classif_pipeline.pkl.z' using
joblib.dump(pipeline, 'classif_pipeline.pkl.z').
The problem is when I try to load this file in a different computer I get the error message bellow.
Code first:
import joblib
full_pipeline = joblib.load('classif_pipeline.pkl.z')
Error message. Also, I have the same version of Scikit Learn and joblib in this pc too.
Traceback (most recent call last):
File "/media/backup/programming/python/jupyter/classification/main.py", line 3, in <module>
full_pipeline = joblib.load('classif_pipeline.pkl.z')
File "/home/guilherme/.local/lib/python3.10/site-packages/joblib/numpy_pickle.py", line 658, in load
obj = _unpickle(fobj, filename, mmap_mode)
File "/home/guilherme/.local/lib/python3.10/site-packages/joblib/numpy_pickle.py", line 577, in _unpickle
obj = unpickler.load()
File "/usr/lib/python3.10/pickle.py", line 1213, in load
dispatch[key[0]](self)
File "/usr/lib/python3.10/pickle.py", line 1538, in load_stack_global
self.append(self.find_class(module, name))
File "/usr/lib/python3.10/pickle.py", line 1582, in find_class
return _getattribute(sys.modules[module], name)[0]
File "/usr/lib/python3.10/pickle.py", line 331, in _getattribute
raise AttributeError("Can't get attribute {!r} on {!r}"
AttributeError: Can't get attribute 'DependentsImputer' on <module '__main__' from '/media/backup/programming/python/jupyter/classification/main.py'>
DependentsImputer is one of the many other classes I implemented in the Jupyter notebook.
How can I load this file?

TypeError with docplex 2.10.15x

After switching from docplex 2.9.141 to version 2.10.150 (same with 2.10.151) I started having the following error when trying to call solve on my model (using my docloud credentials).
I expect that this problem is caused by the fact that the class ProgressData has been changed to a namedtuple subclass and no longer has default attribute values.
File ".../venv/lib/python3.7/site-packages/docplex/mp/model.py", line 4018, in solve
return self._solve_cloud(context, lex_mipstart)
File ".../venv/lib/python3.7/site-packages/docplex/mp/model.py", line 4227, in _solve_cloud
new_solution = docloud_engine.solve(self, parameters=parameters, lex_mipstart=lex_mipstart)
File ".../venv/lib/python3.7/site-packages/docplex/mp/docloud_engine.py", line 632, in solve
info_to_monitor={'jobid', 'progress'})
File ".../venv/lib/python3.7/site-packages/docplex/mp/docloud_connector.py", line 251, in submit_model_data
info_to_monitor=info_to_monitor)
File ".../venv/lib/python3.7/site-packages/docplex/mp/docloud_connector.py", line 442, in wait_for_completion
loop.start(**kwargs)
File ".../venv/lib/python3.7/site-packages/docplex/mp/utils.py", line 688, in start
self.scheduler.run()
File "/opt/anaconda/lib/python3.7/sched.py", line 151, in run
action(*argument, **kwargs)
File ".../venv/lib/python3.7/site-packages/docplex/mp/utils.py", line 673, in <lambda>
lambda a: self._process_task(a), (task.id,))
File ".../venv/lib/python3.7/site-packages/docplex/mp/utils.py", line 678, in _process_task
task.action(*task.argument)
File ".../venv/lib/python3.7/site-packages/docplex/mp/docloud_connector.py", line 373, in progress_poll
progress_data = self.map_job_info_to_progress_data(info)
File ".../venv/lib/python3.7/site-packages/docplex/mp/docloud_connector.py", line 485, in map_job_info_to_progress_data
pg = ProgressData()
TypeError: __new__() missing 10 required positional arguments: 'id', 'has_incumbent', 'current_objective', 'best_bound', 'mip_gap', 'current_nb_iterations', 'current_nb_nodes', 'remaining_nb_nodes', 'time', and 'det_time'
Is this a bug that has been introduced in the new version or could I be doing anything wrong?
docplex 2.10.154 should fix this. Can you please update and try again ? And sorry for the inconvenience.
This is a bug - I'll deploy a fix asap

spaCy produces "PicklingError: Could not pickle object as excessively deep recursion required." after the first run (second run onwards)

I currently work on spaCy on Spyder3 editor.
But after the first run of below simple code. It gives me the error
"PicklingError: Could not pickle object as excessively deep recursion required."
Could you help me resolve the issue? Am I missing any additional code or settings?
Thanks,
Saul
I could run the code with no issue on jupyter notebook, but it fails on Spyder3.
import spacy
nlp = spacy.load('en_core_web_sm')
It should run without any error.
I have already installed 'en_core_web_sm'
I am not sure what the problem is.
please find whole error message below.
Reloaded modules: __mp_main__, spacy, thinc, thinc.about, thinc.neural, thinc.neural._classes, thinc.neural._classes.model, srsly, srsly._json_api, srsly.ujson, srsly.ujson.ujson, srsly.util, srsly._msgpack_api, srsly.msgpack, srsly.msgpack._version, srsly.msgpack.exceptions, srsly.msgpack._packer, srsly.msgpack._unpacker, srsly.msgpack._ext_type, srsly.msgpack._msgpack_numpy, srsly._pickle_api, srsly.cloudpickle, srsly.cloudpickle.cloudpickle, thinc.neural.util, thinc.neural.train, tqdm, tqdm._tqdm, tqdm._utils, tqdm._monitor, tqdm._tqdm_gui, tqdm._tqdm_pandas, tqdm._main, tqdm._version, thinc.neural.optimizers, thinc.neural.ops, thinc.neural.mem, thinc.check, thinc.compat, thinc.extra, thinc.extra.wrapt, thinc.extra.wrapt.wrappers, thinc.extra.wrapt._wrappers, thinc.extra.wrapt.decorators, thinc.extra.wrapt.importer, thinc.exceptions, wasabi, wasabi.printer, wasabi.tables, wasabi.util, wasabi.traceback, spacy.cli, spacy.cli.download, plac, plac_core, plac_ext, spacy.cli.link, spacy.compat, spacy.util, pkg_resources, pkg_resources.extern, pkg_resources._vendor, pkg_resources.extern.six, pkg_resources.py31compat, pkg_resources.extern.appdirs, pkg_resources._vendor.packaging.__about__, pkg_resources.extern.packaging, pkg_resources.extern.packaging.version, pkg_resources.extern.packaging._structures, pkg_resources.extern.packaging.specifiers, pkg_resources.extern.packaging._compat, pkg_resources.extern.packaging.requirements, pkg_resources.extern.pyparsing, pkg_resources.extern.packaging.markers, jsonschema, jsonschema.exceptions, attr, attr.converters, attr._make, attr._config, attr._compat, attr.exceptions, attr.filters, attr.validators, attr._funcs, jsonschema._utils, jsonschema.compat, jsonschema._format, jsonschema._types, pyrsistent, pyrsistent._pmap, pyrsistent._compat, pyrsistent._pvector, pyrsistent._transformations, pvectorc, pyrsistent._pset, pyrsistent._pbag, pyrsistent._plist, pyrsistent._pdeque, pyrsistent._checked_types, pyrsistent._field_common, pyrsistent._precord, pyrsistent._pclass, pyrsistent._immutable, pyrsistent._helpers, pyrsistent._toolz, jsonschema.validators, jsonschema._legacy_validators, jsonschema._validators, spacy.symbols, spacy.errors, spacy.about, spacy.cli.info, spacy.cli.package, spacy.cli.profile, thinc.extra.datasets, thinc.extra._vendorized, thinc.extra._vendorized.keras_data_utils, thinc.extra._vendorized.keras_generic_utils, spacy.cli.train, spacy._ml, thinc.v2v, thinc.neural._classes.affine, thinc.describe, thinc.neural._classes.relu, thinc.neural._classes.maxout, thinc.neural._classes.softmax, thinc.neural._classes.selu, thinc.i2v, thinc.neural._classes.hash_embed, thinc.neural._lsuv, thinc.neural._classes.embed, thinc.neural._classes.static_vectors, thinc.extra.load_nlp, thinc.t2t, thinc.neural._classes.convolution, thinc.neural._classes.attention, thinc.neural._classes.rnn, thinc.api, thinc.neural._classes.function_layer, thinc.neural._classes.feed_forward, thinc.t2v, thinc.neural.pooling, thinc.misc, thinc.neural._classes.batchnorm, thinc.neural._classes.layernorm, thinc.neural._classes.resnet, thinc.neural._classes.feature_extracter, thinc.linear, thinc.linear.linear, spacy.attrs, spacy.gold, spacy.cli.pretrain, spacy.tokens, spacy.tokens.doc, spacy.tokens.token, spacy.tokens.span, spacy.cli.debug_data, spacy.cli.evaluate, spacy.displacy, spacy.displacy.render, spacy.displacy.templates, spacy.cli.convert, spacy.cli.converters, spacy.cli.converters.conllu2json, spacy.cli.converters.iob2json, spacy.cli.converters.conll_ner2json, spacy.cli.converters.jsonl2json, spacy.cli.init_model, preshed, preshed.about, preshed.counter, spacy.vectors, spacy.cli.validate, spacy.glossary
Traceback (most recent call last):
File "<ipython-input-5-e0e768bc0aee>", line 1, in <module>
runfile('/home/saul/pythontraining/NLP/itemWork_3.py', wdir='/home/saul/pythontraining/NLP')
File "/home/saul/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 786, in runfile
execfile(filename, namespace)
File "/home/saul/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/saul/pythontraining/NLP/itemWork_3.py", line 11, in <module>
nlp = spacy.load('en_core_web_sm')
File "/home/saul/.local/lib/python3.7/site-packages/spacy/__init__.py", line 27, in load
return util.load_model(name, **overrides)
File "/home/saul/.local/lib/python3.7/site-packages/spacy/util.py", line 131, in load_model
return load_model_from_package(name, **overrides)
File "/home/saul/.local/lib/python3.7/site-packages/spacy/util.py", line 152, in load_model_from_package
return cls.load(**overrides)
File "/home/saul/anaconda3/lib/python3.7/site-packages/en_core_web_sm/__init__.py", line 12, in load
return load_model_from_init_py(__file__, **overrides)
File "/home/saul/.local/lib/python3.7/site-packages/spacy/util.py", line 190, in load_model_from_init_py
return load_model_from_path(data_path, meta, **overrides)
File "/home/saul/.local/lib/python3.7/site-packages/spacy/util.py", line 173, in load_model_from_path
return nlp.from_disk(model_path)
File "/home/saul/.local/lib/python3.7/site-packages/spacy/language.py", line 791, in from_disk
util.from_disk(path, deserializers, exclude)
File "/home/saul/.local/lib/python3.7/site-packages/spacy/util.py", line 630, in from_disk
reader(path / key)
File "/home/saul/.local/lib/python3.7/site-packages/spacy/language.py", line 787, in <lambda>
deserializers[name] = lambda p, proc=proc: proc.from_disk(p, exclude=["vocab"])
File "pipes.pyx", line 617, in spacy.pipeline.pipes.Tagger.from_disk
File "/home/saul/.local/lib/python3.7/site-packages/spacy/util.py", line 630, in from_disk
reader(path / key)
File "pipes.pyx", line 599, in spacy.pipeline.pipes.Tagger.from_disk.load_model
File "pipes.pyx", line 512, in spacy.pipeline.pipes.Tagger.Model
File "/home/saul/.local/lib/python3.7/site-packages/spacy/_ml.py", line 513, in build_tagger_model
pretrained_vectors=pretrained_vectors,
File "/home/saul/.local/lib/python3.7/site-packages/spacy/_ml.py", line 363, in Tok2Vec
embed >> convolution ** conv_depth, pad=conv_depth
File "/home/saul/.local/lib/python3.7/site-packages/thinc/check.py", line 131, in checker
return wrapped(*args, **kwargs)
File "/home/saul/.local/lib/python3.7/site-packages/thinc/neural/_classes/model.py", line 281, in __pow__
return self._operators["**"](self, other)
File "/home/saul/.local/lib/python3.7/site-packages/thinc/api.py", line 117, in clone
layers.append(copy.deepcopy(orig))
File "/home/saul/anaconda3/lib/python3.7/copy.py", line 169, in deepcopy
rv = reductor(4)
File "/home/saul/.local/lib/python3.7/site-packages/thinc/neural/_classes/model.py", line 96, in __getstate__
return srsly.pickle_dumps(self.__dict__)
File "/home/saul/.local/lib/python3.7/site-packages/srsly/_pickle_api.py", line 14, in pickle_dumps
return cloudpickle.dumps(data, protocol=protocol)
File "/home/saul/.local/lib/python3.7/site-packages/srsly/cloudpickle/cloudpickle.py", line 954, in dumps
cp.dump(obj)
File "/home/saul/.local/lib/python3.7/site-packages/srsly/cloudpickle/cloudpickle.py", line 288, in dump
raise pickle.PicklingError(msg)
PicklingError: Could not pickle object as excessively deep recursion required.
Yup, sure enough, your read operation is attempting
a failed write operation (serialization) behind the scenes,
most curious.
What you are attempting is absolutely vanilla,
and comes straight from the docs.
It certainly should work.
Sorry, but I can't reproduce this on my Mac.
I used conda 4.7.5 to install spacy 2.0.12
(conda install spacy),
which brought in thinc 6.10.3 as a dep.
From where Tok2Vec appears in the stack trace line numbers,
it is clear to me that you are running some different version.
Once I've asked spacy to download it,
spacy.load('en_core_web_sm') works flawlessly.
Your call stack goes from spacy to thinc to srsly.
I do not have srsly installed.
If I pip install srsly it pulls in 0.0.7,
and has no effect on subsequent successful spacy.load()
operations.
Recommend wiping your environment and doing a clean
conda install spacy, there's a fair chance that
will remedy the situation.
versionitis
The thinc rel notes point out this change in 7.0.0:
Use srsly for serialization.
Asking conda to install downrev spacy,
or to downrev one of those two deps,
may change the srsly interaction
and hence change your symptom.
Once you better understand the situation,
perhaps by seeing a successful .load(),
you may want to file a bug report against an affected project.

when i run my code in pycharm it is working fine but when i try to run the same code from server i am getting error

hi i am new to selenium automation .
The code which i have written is working fine and executing in both pycharm and command prompt but when i try to execute the same code from remote server i am getting the below error .
I understand it as do something with pynput but i am not able to figure out how to solve it .
If anyone know about it please help me out .
File "/home/manoj/forum-test-suite/test_forum_page/PageObject/common_methods.py", line 6, in <module>
from pynput.keyboard import Key,Controller
File "/home/manoj/newenv/lib/python3.6/site-packages/pynput/__init__.py", line 23, in <module>
from . import keyboard
File "/home/manoj/newenv/lib/python3.6/site-packages/pynput/keyboard/__init__.py", line 49, in <module>
from ._xorg import KeyCode, Key, Controller, Listener
File "/home/manoj/newenv/lib/python3.6/site-packages/pynput/keyboard/_xorg.py", line 39, in <module>
from pynput._util.xorg import (
File "/home/manoj/newenv/lib/python3.6/site-packages/pynput/_util/xorg.py", line 40, in <module>
_check()
File "/home/manoj/newenv/lib/python3.6/site-packages/pynput/_util/xorg.py", line 38, in _check
display = Xlib.display.Display()
File "/home/manoj/newenv/lib/python3.6/site-packages/Xlib/display.py", line 89, in __init__
self.display = _BaseDisplay(display)
File "/home/manoj/newenv/lib/python3.6/site-packages/Xlib/display.py", line 71, in __init__
protocol_display.Display.__init__(self, *args, **keys)
File "/home/manoj/newenv/lib/python3.6/site-packages/Xlib/protocol/display.py", line 167, in __init__
raise error.DisplayConnectionError(self.display_name, r.reason)
The issue is with respect to the pynput , you need to go to your .bashprofile and append
the pynput path , in PYTHONPATH that you have set .By doing this i was able to solve this issue.

py2app TypeError: dyld_find() got an unexpected keyword argument 'loader'

I'm having difficulty building my app using py2app. I can build it in alias mode without issue using this command:
python3.4 setup.py py2app -A
However when I try and build it using:
python3.4 setup.py py2app
I get the error message as per title of this post. From the research I've done I believe it's an issue with Pillow; however I need Pillow for this app. (Unless there's another module I can use to import images??). I've also tried cx_freeze without success.
Any help or direction much appreciated.
Full traceback as follows:
Traceback (most recent call last):
File "setup.py", line 19, in <module>
setup_requires=['py2app'],
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
distutils/core.py", line 148, in setup dist.run_commands()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
distutils/dist.py", line 955, in run_commands self.run_command(cmd)
File"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
distutils/dist.py", line 974, in run_command cmd_obj.run()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site- packages/py2app/build_app.py", line 659, in run self._run()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site- packages/py2app/build_app.py", line 865, in _run self.run_normal()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site- packages/py2app/build_app.py", line 959, in
run_normal self.create_binaries(py_files, pkgdirs, extensions,loader_files)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site- packages/py2app/build_app.py", line 1214, in create_binaries
platfiles = mm.run()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site-packages/macholib/MachOStandalone.py", line 105, in run
mm.run_file(fn)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site-packages/macholib/MachOGraph.py", line 84, in run_file
self.scan_node(m)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site-packages/macholib/MachOGraph.py", line 110, in scan_node
m = self.load_file(filename, caller=node)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site-packages/macholib/MachOGraph.py", line 93, in load_file
newname = self.locate(name, loader=caller)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site-packages/macholib/MachOStandalone.py", line 23, in locate
newname = super(FilteredMachOGraph, self).locate(filename, loader)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site-packages/macholib/MachOGraph.py", line 49, in locate
loader=loader.filename)
TypeError: dyld_find() got an unexpected keyword argument 'loader'
This is a temporary solution, and it may break other things. I would recommend doing this is a virtual environment so you don't mess up your regular packages. This worked for me (virtenv is the name of my virtual environment)
Open the file /virtenv/lib/python3.4/site-packages/macholib/dyld.py and replace each instance of loader_path with loader. Now save and try again.
You can monkey-patch macholib rather than modifying it in site-packages. Place the following file to the directory containing setup_py2app.py and add import macholib_patch to the top.
macholib_patch.py:
"""
Monkey-patch macholib to fix "dyld_find() got an unexpected keyword argument 'loader'".
Add 'import macholib_patch' to the top of set_py2app.py
"""
import macholib
#print("~"*60 + "macholib verion: "+macholib.__version__)
if macholib.__version__ <= "1.7":
print("Applying macholib patch...")
import macholib.dyld
import macholib.MachOGraph
dyld_find_1_7 = macholib.dyld.dyld_find
def dyld_find(name, loader=None, **kwargs):
#print("~"*60 + "calling alternate dyld_find")
if loader is not None:
kwargs['loader_path'] = loader
return dyld_find_1_7(name, **kwargs)
macholib.MachOGraph.dyld_find = dyld_find

Resources