I trying to write a script but I don't understand the following behavior.
Let's say I have script.py containing
import sys
import subprocess
result = subprocess.run(["flatpak", "list", "--columns=application", "--app"], check=True, stdout=subprocess.PIPE)
somestring = str(result.stdout, 'utf-8')
print(somestring)
And from interactive shell I run :
>>> import script
This works perfectly.
But then, if I have this file instead (called func.py) :
import sys
import subprocess
def GetInstalledApps():
result = subprocess.run(["flatpak", "list", "--columns=application", "--app"], check=True, stdout=subprocess.PIPE)
somestring = str(result.stdout, 'utf-8')
print(somestring)
If I jump into python interactive shell and run
>>> import func # this call succeed
>>> func.GetInstalledApps() # this throw the following error :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/path/to/func.py", line 11, in GetInstalledApps
somestring = str(result.stdout, 'utf-8')
^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: a bytes-like object is required, not 'str'
I understand what the error means, but I don't understand it is thrown.
import funcin an interactive shell that had ALREADY imported func? if so, try again in a new shellpython. My python version is 3.11.4