UnicodeDecodeError when opening Python in console [duplicate] - python

I got this error when running python from command line on Windows 10:
C:\Users\windows> python
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Failed calling sys.__interactivehook__
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site.py", line 410, in register_readline
readline.read_history_file(history)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyreadline\rlmain.py", line 165, in read_history_file
self.mode._history.read_history_file(filename)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyreadline\lineeditor\history.py", line 82, in read_history_file
for line in open(filename, 'r'):
File "C:\ProgramData\Anaconda3\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 991: character maps to <undefined>
I have not changed anything about my python environment, I was just running some scripts that I've been working on for the last week. I honestly have no idea where this could be coming from; it seems to be an issue when python reads in its history. Updating python and conda did nothing. I found a very similar issue on a Chinese site here but the solution is unhelpful.
At the moment, I cannot find any issues when continuing to run python or my scripts, but I don't want this to come back and bite me. Any help is appreciated!

As user metatoaster commented, the python history file contains bytes that cannot be decoded by your system's default encoding and may be deleted. If the file contains commands that you want to keep or review you can take a backup first and try reviewing its contents in a text editor.
You have a history file that contain characters that cannot be decoded. Remove the history file (POSIX: ~/.python_history, on Windows it might be %userprofile%/.python_history) and see if that problem goes away.

in 'history.py' file, try to change "for line in open(filename, 'r'):" to "for line in open(filename, 'r', encoding='utf-8'):"

Related

Anaconda prompt giving an error on opening

I tried to install anaconda 2.7 on my new computer(running win10), but failed.
I had it installed on my old laptop (running win7), and I was using spyder happily. Now after the install, the Navigator refuses to open, and the anaconda prompt gives an error first hand when opening.
The error i am receiving:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda2\Scripts\conda-script.py", line 12, in
sys.exit(main())
File "C:\ProgramData\Anaconda2\lib\site-packages\conda\cli\main.py", line 145, in main
init_loggers()
File "C:\ProgramData\Anaconda2\lib\site-packages\conda\cli\main.py", line 56, in init_loggers
from ..gateways.logging import initialize_logging, set_verbosity
File "C:\ProgramData\Anaconda2\lib\site-packages\conda\gateways\logging.py", line 14, in
from ..common.io import attach_stderr_handler
File "C:\ProgramData\Anaconda2\lib\site-packages\conda\common\io.py", line 580, in
class time_recorder(ContextDecorator): # pragma: no cover
File "C:\ProgramData\Anaconda2\lib\site-packages\conda\common\io.py", line 581, in time_recorder
record_file = get_instrumentation_record_file()
File "C:\ProgramData\Anaconda2\lib\site-packages\conda\common\io.py", line 577, in get_instrumentation_record_file
return expand(os.environ.get("CONDA_INSTRUMENTATION_RECORD_FILE", default_record_file))
File "C:\ProgramData\Anaconda2\lib\site-packages\conda\common\path.py", line 49, in expand
return abspath(expanduser(expandvars(path))
enter code here
File "C:\ProgramData\Anaconda2\lib\ntpath.py", line 311, in expanduser
return userhome + path[i:]
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe1 in position 13: invalid continuation byte
I tried reinstalling anaconda completely, tried both 64 and 32 bit versions, tried miniconda, same error all the time.
Would really appreciate any help!
Seems to be related to non-utf-8 characters in your home directory.
See a similar issues for 'ascii' codec:
https://github.com/conda/conda/issues/1631
https://github.com/conda/conda/issues/1180
If you need to use Python 2 it is suggested to use Anaconda3 and create a Python 2 environment from that.

Python subprocess.call fails on windows without shell=True

I am using Python 2.7.14 on windows 10 64bit. I need to use
subprocess.call
but it is failing everytime if I will not pass shell=True. As per the python doc, shell=True should not be used. Also I want to know why it is failing.
python
ActivePython 2.7.14.2717 (ActiveState Software Inc.) based on
Python 2.7.14 (default, Dec 15 2017, 16:31:45) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call(['date'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\subprocess.py", line 168, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 390, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
But when run with shell=True, it is running fine
>>> subprocess.call(['date'],shell=True)
The current date is: Wed 12/04/2019
Enter the new date: (mm-dd-yy)
I tried on mac too and subprocess.call is running fine without shell=True
Please help.
date is not an executable, but a builtin function of cmd.exe. You cannot use it outside the shell.

.pyw program crashes upon execution, works as normal .py

I have a python program that I want to run without the console popping up. Naturally I went for a .pyw file to hide the console. However I noticed that after converting the file to a .pyw file. It would crash immediately upon executing.
I have tried uninstalling and reinstalling python and Flask. I ran the program within the Python IDLE and was finally able to get an error readout. I then looked it up and the answer ended up being "you have to run it in py". However the file runs completely fine on my desktop but not on my laptop or any other device that I have tried when set to a .pyw. This is the error traceback:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
=========== RESTART: C:\Users\emidd\OneDrive\Desktop\FlaskApp.pyw ===========
Traceback (most recent call last):
File "C:\Users\emidd\OneDrive\Desktop\FlaskApp.pyw", line 16, in <module>
app.run()
File "C:\Users\emidd\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 985, in run
cli.show_server_banner(self.env, self.debug, self.name, False)
File "C:\Users\emidd\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\cli.py", line 670, in show_server_banner
click.echo(message)
File "C:\Users\emidd\AppData\Local\Programs\Python\Python37\lib\site-packages\click\utils.py", line 218, in echo
file = _default_text_stdout()
File "C:\Users\emidd\AppData\Local\Programs\Python\Python37\lib\site-packages\click\_compat.py", line 675, in func
rv = wrapper_func()
File "C:\Users\emidd\AppData\Local\Programs\Python\Python37\lib\site-packages\click\_compat.py", line 436, in get_text_stdout
rv = _get_windows_console_stream(sys.stdout, encoding, errors)
File "C:\Users\emidd\AppData\Local\Programs\Python\Python37\lib\site-packages\click\_winconsole.py", line 295, in _get_windows_console_stream
func = _stream_factories.get(f.fileno())
io.UnsupportedOperation: fileno
I know that it can work because I have it running on my desktop, however I am not sure why it works on it but not my laptop. When it works, the readout on the IDLE is "running on http://127.0.0.1:5000/" if anyone could help me get this working with Pythonw that would be great. However it does technically work with normal python.
Try running it on the commandline, IDLE has different IO objects which can mess with it.

Python 3.5.1 IDLE keeps crashing on Mac OSX 10.11.4

I'm running IDLE with python 3.5.1 by python.org, but it keeps crashing.
The shell starts with
"WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable.
Visit http://www.python.org/download/mac/tcltk/ for current information."
As recommended on that site I installed ActiveTcl 8.5.18.0, but that doesn't seem to have helped.
This message is relatively new, maybe since the OSX update to 10.11.4.
Any suggestions what might cause these crashes?
Thanks
#OSX 10.11.4 MacBook Air 13" 2013, i7, 8GB Ram
Update:
I called Idle via Terminal with python3 -m idlelib and then About Idle. It says Python and IDLE Version 3.5.1 an Tk Version 8.5.9.
I also noticed that when I click on README in the IDLE section, it prints an error on the terminal:
Exception in Tkinter callback
Traceback (most recent call last):
File"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1549, in __call__
return self.func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/idlelib/aboutDialog.py", line 127, in ShowIDLEAbout
self.display_file_text('About - Readme', 'README.txt')
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/idlelib/aboutDialog.py", line 139, in display_file_text
textView.view_file(self, title, fn, encoding) File"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/idlelib/textView.py", line 74, in view_file
contents = file.read()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 72: invalid start byte
For me, 90% of IDLE crashes were caused by typing a left-parenthesis — very annoying.
In IDLE, you can fix it by:
Options Menu > Configure Extensions > CallTips > set to FALSE
Then restart.

PyDev and Django: PyDev breaking Django shell?

I've set up a new project, and populated it with simple models. (Essentially I'm following the tut.)
When I run python manage.py shell on the command line, it works fine:
>python manage.py shell
Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from mysite.myapp.models import School
>>> School.objects.all()
[]
Works great. Then, I try to do the same thing in Eclipse (using a Django project that is composed of the same files.)
Right click on mysite project >>
Django >> Shell with Django
environment
This is the output from the PyDev Console:
>>> import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))
C:\Python26\python.exe 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)]
>>>
>>> from django.core import management;import mysite.settings as settings;management.setup_environ(settings)
'path\\to\\mysite'
>>> from mysite.myapp.models import School
>>> School.objects.all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python26\lib\site-packages\django\db\models\query.py", line 68, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "C:\Python26\lib\site-packages\django\db\models\query.py", line 83, in __len__
self._result_cache.extend(list(self._iter))
File "C:\Python26\lib\site-packages\django\db\models\query.py", line 238, in iterator
for row in self.query.results_iter():
File "C:\Python26\lib\site-packages\django\db\models\sql\query.py", line 287, in results_iter
for rows in self.execute_sql(MULTI):
File "C:\Python26\lib\site-packages\django\db\models\sql\query.py", line 2368, in execute_sql
cursor = self.connection.cursor()
File "C:\Python26\lib\site-packages\django\db\backends\__init__.py", line 81, in cursor
cursor = self._cursor()
File "C:\Python26\lib\site-packages\django\db\backends\sqlite3\base.py", line 170, in _cursor
self.connection = Database.connect(**kwargs)
OperationalError: unable to open database file
What am I doing wrong here?
The error is about not being open the database file. So I'd guess that the path to the database in your settings.py is a relative path, and PyDev starts the shell with a different current directory than you normally use.
If this is the case, change the DATABASE_NAME setting to an absolute path and it should work.

Categories

Resources