Python Forum
UnicodeEncodeError caused by print when program runs from Popen - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: UnicodeEncodeError caused by print when program runs from Popen (/thread-36042.html)



UnicodeEncodeError caused by print when program runs from Popen - SheeppOSU - Jan-12-2022

I am running a python program from another python program using subprocess.Popen, specifically like this subprocess.Popen(['python', self.path], stdout=subprocess.PIPE, stderr=subprocess.PIPE). I also tried specifying the encoding and errors parameters like so subprocess.Popen(['python', self.path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8', errors='utf-8'), which did not fix the error.
Here's the entire traceback of the error:
Error:
Traceback (most recent call last): File "main.py", line 768, in <module> bot.loop.run_until_complete(bot.start()) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 616, in run_until_complete return future.result() File "main.py", line 297, in start print(future.result()) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\concurrent\futures\_base.py", line 437, in result return self.__get_result() File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\concurrent\futures\_base.py", line 389, in __get_result raise self._exception File "main.py", line 350, in poll await self.on_message(user, channel, content, tags) File "main.py", line 412, in on_message await self.commands[command](user, channel, args) File "main.py", line 40, in check return await func(self, user, channel, args, *eargs, **kwargs) File "main.py", line 440, in pull await self.send_message(channel, File "main.py", line 378, in send_message print(f"> PRIVMSG #{chnl} :{message}") File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\encodings\cp1252.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode characters in position 60-62: character maps to <undefined>
If I were to run the program from the command line with python, it runs just fine. However, as a subprocess, it seems to have trouble encoding certain characters. I would like some help figuring out the cause of the error and how I can fix it.


RE: UnicodeEncodeError caused by print when program runs from Popen - snippsat - Jan-12-2022

(Jan-12-2022, 07:46 AM)SheeppOSU Wrote: I am running a python program from another python program using subprocess.Popen,
Why are doing this and not using the module/package way?
When doing this you add an extra step and can get problem subprocess uses wrong encoding on Windows


RE: UnicodeEncodeError caused by print when program runs from Popen - SheeppOSU - Jan-12-2022

I have a few bots which are being run on a separate device and I wanted to attempt in making a way to manage all of them remotely. Originally I used a shell script to simply run the bots and restart the device every so often, though this comes with some disadvantages. If I used a python program to start and manage the bots, it would be easy to create some kind of remote access. If there's a better way to achieve this I'd be happy to hear you out!


RE: UnicodeEncodeError caused by print when program runs from Popen - snippsat - Jan-12-2022

Not going down the the module/package way.
Here are two,both run the Python code that path point to
from runpy import run_path

run_path(r"G:\div_code\answer\html_test.py")
# Other way
exec(open(r"G:\div_code\answer\20_last.py").read())



RE: UnicodeEncodeError caused by print when program runs from Popen - SheeppOSU - Jan-12-2022

(Jan-12-2022, 10:28 PM)snippsat Wrote: Here are two,both run the Python code that path point to
Thanks, I'll look into these.


RE: UnicodeEncodeError caused by print when program runs from Popen - SheeppOSU - Jan-13-2022

Thank you for the suggestions, however, neither of these options will work with what I'm trying to achieve. Though, since the error only incurs on windows, it will run on my raspberry pi just fine.