Python Forum
UnicodeEncodeError caused by print when program runs from Popen
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UnicodeEncodeError caused by print when program runs from Popen
#1
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.
Reply
#2
(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
Reply
#3
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!
Reply
#4
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())
Reply
#5
(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.
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to do 100 runs simulation based on the current codes? dududada 6 986 Sep-03-2023, 01:43 PM
Last Post: deanhystad
Question UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 562: ord ctrldan 23 4,814 Apr-24-2023, 03:40 PM
Last Post: ctrldan
  [SOLVED] [Debian] UnicodeEncodeError: 'ascii' codec Winfried 1 1,031 Nov-16-2022, 11:41 AM
Last Post: Winfried
  Another program runs bho68 7 1,186 Nov-08-2022, 08:16 PM
Last Post: bho68
  UnicodeEncodeError - Dealing with Japanese Characters fioranosnake 2 2,454 Jul-07-2022, 08:43 PM
Last Post: fioranosnake
  UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin Armandito 6 2,728 Apr-29-2022, 12:36 PM
Last Post: Armandito
  [UnicodeEncodeError from smtplib] yoohooos 0 3,386 Sep-25-2021, 04:27 AM
Last Post: yoohooos
  Importing a function from another file runs the old lines also dedesssse 6 2,555 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  How to get program output from subprocess.Popen? glestwid 1 2,369 Aug-19-2020, 05:44 AM
Last Post: buran
  Help with getting sub-program to print text djwilson0495 2 1,896 Aug-16-2020, 05:03 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020