which libraries do I need to run an HTTPS API on python? - python

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

Related

'Access is denied' while trying to generate executable using pyinstaller

python = 3.8.0
pyinstaller=5.2
commands
pyinstaller --onefile Main.py
Traceback (most recent call last):
File "C:\Users\Testuser\Documents\installed\Python\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\Testuser\Documents\installed\Python\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "C:\Users\Testuser\Documents\installed\Python\lib\site-packages\PyInstaller\__main__.py", line 188, in <module>
run()
File "C:\Users\Testuser\Documents\installed\Python\lib\site-packages\PyInstaller\__main__.py", line 178, in run
run_build(pyi_config, spec_file, **vars(args))
File "C:\Users\Testuser\Documents\installed\Python\lib\site-packages\PyInstaller\__main__.py", line 59, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "C:\Users\Testuser\Documents\installed\Python\lib\site-packages\PyInstaller\building\build_main.py", line 842, in main
build(specfile, distpath, workpath, clean_build)
File "C:\Users\Testuser\Documents\installed\Python\lib\site-packages\PyInstaller\building\build_main.py", line 764, in build
exec(code, spec_namespace)
File "C:\Users\Testuser\Documents\com\git\testme.spec", line 24, in <module>
exe = EXE(
File "C:\Users\Testuser\Documents\installed\Python\lib\site-packages\PyInstaller\building\api.py", line 524, in __init__
self.__postinit__()
File "C:\Users\Testuser\Documents\installed\Python\lib\site-packages\PyInstaller\building\datastruct.py", line 173, in __postinit__
self.assemble()
File "C:\Users\Testuser\Documents\installed\Python\lib\site-packages\PyInstaller\building\api.py", line 692, in assemble
self.manifest.update_resources(build_name, [1])
File "C:\Users\Testuser\Documents\installed\Python\lib\site-packages\PyInstaller\utils\win32\winmanifest.py", line 904, in update_resources
UpdateManifestResourcesFromXML(dstpath, self.toprettyxml().encode("UTF-8"), names, languages)
File "C:\Users\Testuser\Documents\installed\Python\lib\site-packages\PyInstaller\utils\win32\winmanifest.py", line 992, in UpdateManifestResourcesFromXML
winresource.UpdateResources(dstpath, xmlstr, RT_MANIFEST, names or [name], languages or [0, "*"])
File "C:\Users\Testuser\Documents\installed\Python\lib\site-packages\PyInstaller\utils\win32\winresource.py", line 189, in UpdateResources
win32api.EndUpdateResource(hdst, 0)
File "C:\Users\Testuser\Documents\installed\Python\lib\site-packages\win32ctypes\pywin32\win32api.py", line 228, in EndUpdateResource
with _pywin32error():
File "C:\Users\Testuser\Documents\installed\Python\lib\contextlib.py", line 153, in __exit__
self.gen.throw(typ, value, traceback)
File "C:\Users\Testuser\Documents\installed\Python\lib\site-packages\win32ctypes\pywin32\pywintypes.py", line 37, in pywin32error
raise error(exception.winerror, exception.function, exception.strerror)
win32ctypes.pywin32.pywintypes.error: (5, 'EndUpdateResource', 'Access is denied')

Python list attribute error without a list

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

Multiprocessing ProcessPoolExecutor - BrokenProcessPool Error when run in Windows command line

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

Python not catching UnicodeEncodeError from emojis

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

How to edit jupyter nbconvert templates

I'm currently trying to change the looks of the nbconvert-output in jupyter. To be more precise, I want to convert a ipynb to latex but not using the default tamplate. The thing is I don't know how to edit the files to get the result I want.
Lets say I want to use the "style_notebook.tplx" so I need to donwload ipython_nbconvert_config.py and use_cell_style.tplx. After that I need to change in the use_cell_style file the second line to ((* set cell_style = 'style_notebook.tplx' *)). But to be honest I don't know with what to edit a .tplx File (I used just Editor from Windows)....
After changing that and moving the two files to the same directonary as my .ipynb is, I tried this in the command prompt:
jupyter nbconvert --config ipython_nbconvert_config --to latex filename.ipynb
and got the error:
[NbConvertApp] WARNING | Error loading config file: ipython_nbconvert_config
Traceback (most recent call last):
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\site-packages\jupyter_core\application.py", line 202, in load_config_file
path=path
File "<decorator-gen-5>", line 2, in load_config_file
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\site-packages\traitlets\config\application.py", line 87, in catch_config_error
return method(app, *args, **kwargs)
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\site-packages\traitlets\config\application.py", line 603, in load_config_file
self.update_config(new_config)
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\site-packages\traitlets\config\configurable.py", line 198, in update_config
self._load_config(config)
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\site-packages\traitlets\config\configurable.py", line 168, in _load_config
warn(msg)
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\contextlib.py", line 89, in __exit__
next(self.gen)
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\site-packages\traitlets\traitlets.py", line 1131, in hold_trait_notifications
self.notify_change(change)
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\site-packages\traitlets\traitlets.py", line 1176, in notify_change
c(change)
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\site-packages\nbconvert\nbconvertapp.py", line 227, in _postprocessor_class_changed
self.postprocessor_factory = import_item(new)
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\site-packages\traitlets\utils\importstring.py", line 42, in import_item
return __import__(parts[0])
ModuleNotFoundError: No module named 'PDF'
Traceback (most recent call last):
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\Sito\AppData\Local\Programs\Python\Python36-32\Scripts\jupyter-nbconvert.EXE\__main__.py", line 9, in <module>
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\site-packages\jupyter_core\application.py", line 267, in launch_instance
return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\site-packages\traitlets\config\application.py", line 657, in launch_instance
app.initialize(argv)
File "<decorator-gen-7>", line 2, in initialize
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\site-packages\traitlets\config\application.py", line 87, in catch_config_error
return method(app, *args, **kwargs)
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\site-packages\nbconvert\nbconvertapp.py", line 252, in initialize
self.init_postprocessor()
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\site-packages\nbconvert\nbconvertapp.py", line 298, in init_postprocessor
self._postprocessor_class_changed({'new': self.postprocessor_class})
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\site-packages\nbconvert\nbconvertapp.py", line 227, in _postprocessor_class_changed
self.postprocessor_factory = import_item(new)
File "c:\users\Sito\appdata\local\programs\python\python36-32\lib\site-packages\traitlets\utils\importstring.py", line 42, in import_item
return __import__(parts[0])
ModuleNotFoundError: No module named 'PDF'
So it says that I miss the PDF-module, but I'm not sure hows that possible, because until now I could convert to pdf just fine. So I aussume I made some mistakes in changing the files...
Would be nice if someone could explain how to fix this problem..
Thanks, Sito.

Categories