Previously translating the name with a non-bmp map prevented emojis in names from crashing the script, but suddenly it doesn't work and i don't know why.
I tried adding a try/except block to see what the problem is, but the except doesn't catch the exception at all. I used the debugger and the code just jumps to the line after the except block (!?)
Here is the problematic code:
The line
logger.info(f"Liking tweet from user {name}")
throws the following exception:
--- Logging error ---
Traceback (most recent call last):
File "C:\Users\JGC\anaconda3\lib\logging\__init__.py", line 1028, in emit
stream.write(msg + self.terminator)
File "C:\Users\JGC\anaconda3\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\ufffd' in position 40: character maps to <undefined>
Call stack:
File "C:\Users\JGC\anaconda3\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\Users\JGC\anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "c:\Users\JGC\.vscode\extensions\ms-python.python-2020.6.91350\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
cli.main()
File "c:\Users\JGC\.vscode\extensions\ms-python.python-2020.6.91350\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main
run()
File "c:\Users\JGC\.vscode\extensions\ms-python.python-2020.6.91350\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 267, in run_file
runpy.run_path(options.target, run_name=compat.force_str("__main__"))
File "C:\Users\JGC\anaconda3\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Users\JGC\anaconda3\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Users\JGC\anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "c:\Users\JGC\Desktop\Python\Templates de bots\Twitter Template\posting-bot\machado.py", line 169, in <module>
stream.filter(track=HASHTAGS, languages=["pt"])
File "C:\Users\JGC\anaconda3\lib\site-packages\tweepy\streaming.py", line 474, in filter
self._start(is_async)
File "C:\Users\JGC\anaconda3\lib\site-packages\tweepy\streaming.py", line 389, in _start
self._run()
File "C:\Users\JGC\anaconda3\lib\site-packages\tweepy\streaming.py", line 289, in _run
self._read_loop(resp)
File "C:\Users\JGC\anaconda3\lib\site-packages\tweepy\streaming.py", line 351, in _read_loop
self._data(next_status_obj)
File "C:\Users\JGC\anaconda3\lib\site-packages\tweepy\streaming.py", line 323, in _data
if self.listener.on_data(data) is False:
File "C:\Users\JGC\anaconda3\lib\site-packages\tweepy\streaming.py", line 54, in on_data
if self.on_status(status) is False:
File "c:\Users\JGC\Desktop\Python\Templates de bots\Twitter Template\posting-bot\machado.py", line 59, in on_status
logger.info(f"Liking tweet from user {name}")
Message: 'Liking tweet from user maya | �; aridante'
Arguments: ()
Related
receiver_email = open(subscribers.txt, 'r').readlines()
for i in receiver_email:
current_mail = i.removesuffix("\n")
print(type(current_mail))
print(type(smtp_data[2]))
print(type(message))
smtp.sendmail(smtp_data[2], current_mail, message.as_string())
this previous code throws out an attribute error 'list' object has no attribute 'encode' on last line
the print statements yield out the following:
<class 'str'>
<class 'str'>
<class 'email.mime.multipart.MIMEMultipart'>
Any ideas on why this isn't working?
edit:
here is an example of the contents of the txt file
myemail1#outlook.com
myemail2#hotmail.com
myemail3#gmail.com
fulltraceback:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "c:\Users\Administrator\.vscode\extensions\ms-python.python-2022.8.0\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
cli.main()
File "c:\Users\Administrator\.vscode\extensions\ms-python.python-2022.8.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 444, in main
run()
File "c:\Users\Administrator\.vscode\extensions\ms-python.python-2022.8.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 285, in run_file
runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 269, in run_path
return _run_module_code(code, init_globals, run_name,
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 96, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "c:\Users\Administrator\Desktop\smtp.py", line 62, in <module>
smtp.sendmail(smtp_data[2], current_mail, message.as_string())
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\email\message.py", line 158, in as_string
g.flatten(self, unixfrom=unixfrom)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\email\generator.py", line 116, in flatten
self._write(msg)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\email\generator.py", line 199, in _write
self._write_headers(msg)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\email\generator.py", line 226, in _write_headers
self.write(self.policy.fold(h, v))
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\email\_policybase.py", line 326, in fold
return self._fold(name, value, sanitize=True)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\email\_policybase.py", line 369, in _fold
parts.append(h.encode(linesep=self.linesep, maxlinelen=maxlinelen))
AttributeError: 'list' object has no attribute 'encode'
See below code - you can write it as UTF-8 in current_mail.encode('utf-8')
It turned out that the message.as_string messes up when I am using a list of emails for some reason, I changed it from attaching the email context to a mimetext to directly setting it to mimetext and it seemed to do the trick.
Huge thanks to Barmar
I try to use some libraries including "https://api.coinbase.com/v2/prices/spot?currency=USD"
but i got this error:
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.240.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.240.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "c:\Users\Hessam\.vscode\extensions\ms-python.python-2021.12.1559732655\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
cli.main()
File "c:\Users\Hessam\.vscode\extensions\ms-python.python-2021.12.1559732655\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 444, in main
run()
File "c:\Users\Hessam\.vscode\extensions\ms-python.python-2021.12.1559732655\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 285, in run_file
runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.240.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 268, in run_path
code, fname = _get_code_from_file(run_name, path_name)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.240.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 242, in _get_code_from_file
code = compile(f.read(), fname, 'exec')
File "c:\Users\Hessam\Python code\requests.py", line 3
r = requests.get(https://jsonplaceholder.typicode.com/posts)
^
SyntaxError: invalid syntax
so please guide me on this
thanks in advance
I am using executor.map and storing results in 'results'.
Following code works without any problem on Jupiter notebook. However, it crashes when executing the python script in Windows command prompt.
rop_test.py
print ('start of process')
import concurrent.futures
from funcfile import func
if __name__ == '__main__':
with concurrent.futures.ProcessPoolExecutor() as executor:
results = executor.map(func, varlist1,varlist2)
global master_list
master_list=list(results)
print(master_list)
#do something with master_list
funcfile.py
def func(var1,var2):
var3=var1+var2
return var3
Error
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 114, in _main
prepare(preparation_data)
File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 225, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
run_name="__mp_main__")
File "C:\ProgramData\Anaconda3\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\ProgramData\Anaconda3\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\ProgramData\Anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\Administrator\notebooks\rop_test.py", line 640, in <module>
print (master_list)
NameError: name 'master_list' is not defined
Finished in 0.0 second(s)
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 114, in _main
prepare(preparation_data)
File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 225, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
run_name="__mp_main__")
File "C:\ProgramData\Anaconda3\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\ProgramData\Anaconda3\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\ProgramData\Anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\Administrator\notebooks\rop_test.py", line 640, in <module>
print (master_list)
NameError: name 'master_list' is not defined
Traceback (most recent call last):
File "rop_test.py", line 634, in <module>
master_list=list(results)
File "C:\ProgramData\Anaconda3\lib\concurrent\futures\process.py", line 483, in _chain_from_iterable_of_lists
for element in iterable:
File "C:\ProgramData\Anaconda3\lib\concurrent\futures\_base.py", line 598, in result_iterator
yield fs.pop().result()
File "C:\ProgramData\Anaconda3\lib\concurrent\futures\_base.py", line 428, in result
return self.__get_result()
File "C:\ProgramData\Anaconda3\lib\concurrent\futures\_base.py", line 384, in __get_result
raise self._exception
concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.
I have tried putting this in a main command, adding wait, adding [var for var in results], list(results) etc. but to no avail.
N.B. I am on python 3.6
I want alembic to get the current stored procedure from DB compare it to the one in the code. If they are different to update the DB.
I edited the env.py file to check for this, and generate an upgrade versions of the raw SQL.
when running the following :
writer = rewriter.Rewriter()
#writer.rewrites(ops)
def stored_procedure(context, revision, op):
if not change_detected():
print("did not detect difference in stored procedure.")
return op
else:
print("detected difference in stored")
package_sql = open(migration_file_path, "r")
package_sql = migration_file.read()
package_sql = '"""' + package_sql + '"""'
return [
op,
op.get_bind().execute(package_sql),
]
Traceback (most recent call last):
File "c:\Project\.vscode\extensions\ms-python.python-2019.9.34911\pythonFiles\ptvsd_launcher.py", line 43, in <module>
main(ptvsdArgs)
File "c:\Project\.vscode\extensions\ms-python.python-2019.9.34911\pythonFiles\lib\python\ptvsd\__main__.py", line 432, in main
run()
File "c:\Project\.vscode\extensions\ms-python.python-2019.9.34911\pythonFiles\lib\python\ptvsd\__main__.py", line 316, in run_file
runpy.run_path(target, run_name='__main__')
File "C:\Project\Programs\Python\Python37\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Project\Programs\Python\Python37\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Project\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:/Project/manage.py", line 15, in <module>
manager.run()
...
File "C:\Project\ENV\lib\site-packages\alembic\runtime\migration.py", line 351, in run_migrations
for step in self._migrations_fn(heads, self):
File "C:\Project\ENV\lib\site-packages\alembic\command.py", line 173, in retrieve_migrations
revision_context.run_autogenerate(rev, context)
File "C:\Project\ENV\lib\site-packages\alembic\autogenerate\api.py", line 433, in run_autogenerate
self._run_environment(rev, migration_context, True)
File "C:\Project\ENV\lib\site-packages\alembic\autogenerate\api.py", line 483, in _run_environment
hook(migration_context, rev, self.generated_revisions)
File "migrations\env.py", line 167, in process_revision_directives
stored_procedure(context, revision, directives)
File "migrations\env.py", line 146, in stored_procedure
op.get_bind().execute(package_sql),
AttributeError: 'list' object has no attribute 'get_bind'
I am trying to add Persian language to spaCy. I followed almost everything which was said here. But when I issue the below command
python -m spacy train fa fa-model fa-ud-train.json fa-ud-dev.json --n-iter 25 --no-ner
I get some errors that I don't know how to fix.
$ python -m spacy train fa fa-model fa-ud-train.json fa-ud-dev.json --n-iter 25 --no-ner
Itn. N weight N feats UAS NER F. Tag % Token %
Traceback (most recent call last):
File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/PATH/TO/PROJECT/MY-PY-ENVIRON/lib/python3.5/site-packages/spacy/__main__.py", line 133, in <module>
plac.Interpreter.call(CLI)
File "/PATH/TO/PROJECT/MY-PY-ENVIRON/lib/python3.5/site-packages/plac_ext.py", line 1142, in call
print(out)
File "/PATH/TO/PROJECT/MY-PY-ENVIRON/lib/python3.5/site-packages/plac_ext.py", line 914, in __exit__
self.close(exctype, exc, tb)
File "/PATH/TO/PROJECT/MY-PY-ENVIRON/lib/python3.5/site-packages/plac_ext.py", line 952, in close
self._interpreter.throw(exctype, exc, tb)
File "/PATH/TO/PROJECT/MY-PY-ENVIRON/lib/python3.5/site-packages/plac_ext.py", line 964, in _make_interpreter
arglist = yield task
File "/PATH/TO/PROJECT/MY-PY-ENVIRON/lib/python3.5/site-packages/plac_ext.py", line 1139, in call
raise_(task.etype, task.exc, task.tb)
File "/PATH/TO/PROJECT/MY-PY-ENVIRON/lib/python3.5/site-packages/plac_ext.py", line 53, in raise_
raise exc.with_traceback(tb)
File "/PATH/TO/PROJECT/MY-PY-ENVIRON/lib/python3.5/site-packages/plac_ext.py", line 380, in _wrap
for value in genobj:
File "/PATH/TO/PROJECT/MY-PY-ENVIRON/lib/python3.5/site-packages/plac_ext.py", line 95, in gen_exc
raise_(etype, exc, tb)
File "/PATH/TO/PROJECT/MY-PY-ENVIRON/lib/python3.5/site-packages/plac_ext.py", line 53, in raise_
raise exc.with_traceback(tb)
File "/PATH/TO/PROJECT/MY-PY-ENVIRON/lib/python3.5/site-packages/plac_ext.py", line 966, in _make_interpreter
cmd, result = self.parser.consume(arglist)
File "/PATH/TO/PROJECT/MY-PY-ENVIRON/lib/python3.5/site-packages/plac_core.py", line 207, in consume
return cmd, self.func(*(args + varargs + extraopts), **kwargs)
File "/PATH/TO/PROJECT/MY-PY-ENVIRON/lib/python3.5/site-packages/spacy/__main__.py", line 95, in train
not no_parser, not no_ner, parser_L1)
File "/PATH/TO/PROJECT/MY-PY-ENVIRON/lib/python3.5/site-packages/spacy/cli/train.py", line 40, in train
entity_cfg, n_iter)
File "/PATH/TO/PROJECT/MY-PY-ENVIRON/lib/python3.5/site-packages/spacy/cli/train.py", line 62, in train_model
pos=tagger_cfg, deps=parser_cfg, ner=entity_cfg) as trainer:
File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
return next(self.gen)
File "/PATH/TO/PROJECT/MY-PY-ENVIRON/lib/python3.5/site-packages/spacy/language.py", line 234, in train
self.vocab = self.Defaults.create_vocab(self)
File "/PATH/TO/PROJECT/MY-PY-ENVIRON/lib/python3.5/site-packages/spacy/language.py", line 42, in create_vocab
tag_map=cls.tag_map, lemmatizer=lemmatizer)
File "spacy/vocab.pyx", line 89, in spacy.vocab.Vocab.load (spacy/vocab.cpp:4742)
File "/usr/lib/python3.5/pathlib.py", line 1151, in open
opener=self._opener)
File "/usr/lib/python3.5/pathlib.py", line 1005, in _opener
return self._accessor.open(self, flags, mode)
File "/usr/lib/python3.5/pathlib.py", line 371, in wrapped
return strfunc(str(pathobj), *args)
FileNotFoundError: [Errno 2] No such file or directory: 'fa-model/vocab/strings.json'
I have Persian UniversalDependency treebanks and have converted them to spaCy's expected json format.
I have added fa language to spaCy in MY-PY-ENVIRON virtualenv.
My Environment
installed models: en, en_default
Platform: Linux-4.11.0-14-generic-x86_64-with-LinuxMint-18.2-sonya
Python version: 3.5.2
spaCy version: 1.9.0