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.
Related
I recently started working with Octave on Jupyter notebook. I was previously working on a Linux laptop but then I thought about creating a similar working environment on my Windows laptop. I followed more or less the same approach.
I already have Octave installed on my Windows laptop and I have added C:\Program Files\GNU Octave\Octave-6.1.0\mingw64\bin to the PATH environment variable. I also have set OCTAVE_EXECUTABLE to octave-cli in the environment variables.
I created a virtual environment using the following command.
python -m venv octave
Then I activated the virtual environment from the desired working directory and installed Jupyter in it using the following command.
pip install jupyter
Then I installed the Octave kernel using the following command.
pip install octave-kernel
After that, when I ran the following command to see if things are working or not I got the error mentioned further below.
(octave) λ python -m octave_kernel.check
Octave kernel v0.32.0
Metakernel v0.27.5
Python v3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)]
Python path: C:\Workspace\python-virtual-environments\octave\Scripts\python.exe
Connecting to Octave...
Octave connection established
[WinError 193] %1 is not a valid Win32 application
When I start jupyter-notebook and open a notebook, I end up getting the same error.
[I 15:20:33.465 NotebookApp] Kernel started: 92de579a-c8df-4cf8-9ec4-dbb9aefa1fe5, name: octave
[MetaKernelApp] ERROR | Exception in message handler:
Traceback (most recent call last):
File "c:\workspace\python-virtual-environments\octave\lib\site-packages\ipykernel\kernelbase.py", line 265, in dispatch_shell
yield gen.maybe_future(handler(stream, idents, msg))
File "c:\workspace\python-virtual-environments\octave\lib\site-packages\ipykernel\kernelbase.py", line 655, in kernel_info_request
content.update(self.kernel_info)
File "c:\workspace\python-virtual-environments\octave\lib\site-packages\ipykernel\kernelbase.py", line 648, in kernel_info
'language_info': self.language_info,
File "c:\workspace\python-virtual-environments\octave\lib\site-packages\octave_kernel\kernel.py", line 78, in language_info
'version': self.language_version,
File "c:\workspace\python-virtual-environments\octave\lib\site-packages\octave_kernel\kernel.py", line 69, in language_version
ver = self.octave_engine.eval('version', silent=True)
File "c:\workspace\python-virtual-environments\octave\lib\site-packages\octave_kernel\kernel.py", line 96, in octave_engine
logger=self.log)
File "c:\workspace\python-virtual-environments\octave\lib\site-packages\octave_kernel\kernel.py", line 173, in __init__
self.repl = self._create_repl()
File "c:\workspace\python-virtual-environments\octave\lib\site-packages\octave_kernel\kernel.py", line 392, in _create_repl
force_prompt_on_continuation=True)
File "c:\workspace\python-virtual-environments\octave\lib\site-packages\metakernel\replwrap.py", line 63, in __init__
encoding="utf-8")
File "c:\workspace\python-virtual-environments\octave\lib\site-packages\metakernel\pexpect.py", line 32, in spawn
encoding=encoding, codec_errors=codec_errors)
File "c:\workspace\python-virtual-environments\octave\lib\site-packages\pexpect\popen_spawn.py", line 53, in __init__
self.proc = subprocess.Popen(cmd, **kwargs)
File "C:\Program Files\Python36\lib\subprocess.py", line 729, in __init__
restore_signals, start_new_session)
File "C:\Program Files\Python36\lib\subprocess.py", line 1017, in _execute_child
startupinfo)
OSError: [WinError 193] %1 is not a valid Win32 application
What am I doing wrong here?
Update #1
I tried changing the OCTAVE_EXECUTABLE environment variable to octave but I got the following error.
(octave) λ python -m octave_kernel.check
Octave kernel v0.32.0
Metakernel v0.27.5
Python v3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)]
Python path: C:\Workspace\python-virtual-environments\octave\Scripts\python.exe
Connecting to Octave...
Octave connection established
OCTAVE_EXECUTABLE does not point to an octave-cli file, please see README
It looks like the kernel used does not work with Octave 6. I had the same problem with Octave 6.2.0. OCTAVE_EXECUTABLE and PATH were set correctly according to installation instructions, but without success. The octave kernel chek error message is: [WinError 193] %1 is not a valid Win32 application.
But with Octave 5.2 everything works fine. I work with jupyter under winpython 3.9.2
Problem resolved.
Octave 6.2.0 and winpython 3.9.4 completely reinstalled. Path and environment variable set:
Path —>D:\Octave\Octave-6.2.0\mingw64\bin
OCTAVE_EXECUTABLE —> D:\Octave\Octave-6.2.0\mingw64\bin\octave-cli.exe
now it works suddenly without errors with Octave 6.2.0.
The only difference from the previous installation is winpython 3.9.4 instead of 3.9.2
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'):"
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.
Can anyone explain what's happening here? Even built in cmd.exe commands aren't working:
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call('dir')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Python34\lib\subprocess.py", line 535, in call
with Popen(*popenargs, **kwargs) as p:
File "D:\Python34\lib\subprocess.py", line 848, in __init__
restore_signals, start_new_session)
File "D:\Python34\lib\subprocess.py", line 1104, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
>>>
To use 'dir', you have to pass shell=True:
>>> import subprocess
>>> subprocess.call('dir', shell=True)
You have to do this because dir is built into the shell itself, it's not a standalone console application. This is also mentioned in the subprocess.Popen documentation:
On Windows with shell=True, the COMSPEC environment variable specifies
the default shell. The only time you need to specify shell=True on
Windows is when the command you wish to execute is built into the
shell (e.g. dir or copy). You do not need shell=True to run a batch
file or console-based executable.
When trying to run a python script, I get the error AttributeError: 'module' object has no attribute 'pydebug'. I am using Python 2.6.
Full error:
File "/lib/python2.6/distutils/sysconfig.py", line 238, in get_makefile_filename
return os.path.join(lib_dir, "config" + (sys.pydebug and "_d" or ""), "Makefile")
AttributeError: 'module' object has no attribute 'pydebug'
I hit this issue when attempting to run the Ubuntu 12.04.1 system gdb on a python I built myself. I expect Ubuntu has built some hooks into the system gdb so that it uses a debugging version of Python; but the hooks don't latch onto anything in my own python. I got around this by building my own gdb and running that instead.
Here's the command-line and full traceback:
price#neverland:~/LSST/ip_diffim[master] $ gdb --args python tests/SnapPsfMatch.py
Traceback (most recent call last):
File "/usr/lib/python2.7/site.py", line 562, in <module>
main()
File "/usr/lib/python2.7/site.py", line 544, in main
known_paths = addusersitepackages(known_paths)
File "/usr/lib/python2.7/site.py", line 271, in addusersitepackages
user_site = getusersitepackages()
File "/usr/lib/python2.7/site.py", line 246, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/usr/lib/python2.7/site.py", line 236, in getuserbase
USER_BASE = get_config_var('userbase')
File "/usr/lib/python2.7/sysconfig.py", line 577, in get_config_var
return get_config_vars().get(name)
File "/usr/lib/python2.7/sysconfig.py", line 476, in get_config_vars
_init_posix(_CONFIG_VARS)
File "/usr/lib/python2.7/sysconfig.py", line 337, in _init_posix
makefile = _get_makefile_filename()
File "/usr/lib/python2.7/sysconfig.py", line 331, in _get_makefile_filename
return os.path.join(get_path('platstdlib').replace("/usr/local","/usr",1), "config" + (sys.pydebug and "_d" or ""), "Makefile")
AttributeError: 'module' object has no attribute 'pydebug'
so it seems to go looking for the wrong python (in /usr/lib) despite my having told the system not to do so:
price#neverland:~/LSST/ip_diffim[master] $ which python
/home/price/eups/Linux/python/2.7.2/bin/python
price#neverland:~/LSST/ip_diffim[master] $ echo $PYTHONPATH | grep usr
price#neverland:~/LSST/ip_diffim[master] $
I receive the error when running gdb on a Ubuntu system where an alternative version of Python has been installed and is being preferred by the linker. You can verify whether this is happening in your case by using ldd to ask which libraries gdb is using:
# ldd $(which gdb)
...
libpython2.7.so.1.0 => /usr/local/lib/libpython2.7.so.1.0 (0x00007ff75e044000)
...
You can see that the off-brand version of Python running in /usr/local/lib is supplying libpython to gdb instead of the official Ubuntu Python in /usr/lib which is causing the error — whatever off-brand Python this is in /usr/local must not have been compiled in the same way that the Ubuntu Python was compiled, and so the expectations of gdb are being disappointed.
The solution is to use the environment to control the linker’s behavior and make it prefer the system libpython. For good measure, I also reset my PATH back to something utterly standard. I find that this works:
PATH=/bin:/usr/bin LD_LIBRARY_PATH=/usr/lib gdb ...
I think whatever you are trying to run is expecting to be used with a special debug build of python. sys.pydebug is not normally found on the standard release of the sys module, and I believe it would be there if you built a debug python:
http://docs.python.org/c-api/intro.html#debugging-builds
It is possible that this also might be part of a specific build that Debian/Ubuntu distros are using.
On Ubunut-12.04 pyinstaller built binaries invoke "site.py" from host python installation the call trace is trying to fetch "sys.pydebug" value.
$ python
Python 2.7.3 (default, Feb 27 2014, 19:58:35)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> import sys
>>> sys.pydebug
False
The custom built python is missing this.
HACK: To make pyinstaller binaries work on Ubuntu-12.04.
Added below code change in custom built python which return zero for "sys.pydebug".
$ diff -Naur Python/sysmodule-org.c Python/sysmodule.c
--- Python/sysmodule-org.c 2018-03-15 09:37:26.539515000 -0700
+++ Python/sysmodule.c 2018-03-15 19:58:34.503031000 -0700
## -1106,6 +1106,7 ##
maxunicode -- the largest supported character\n\
builtin_module_names -- tuple of module names built into this
interpreter\n\
version -- the version of this interpreter as a string\n\
+pydebug -- always return zero\n\
version_info -- version information as a named tuple\n\
hexversion -- version information encoded as a single integer\n\
copyright -- copyright notice pertaining to this interpreter\n\
## -1420,6 +1421,8 ##
SET_SYS_FROM_STRING("version",
PyString_FromString(Py_GetVersion()));
+ SET_SYS_FROM_STRING("pydebug",
+ PyInt_FromLong(0));
SET_SYS_FROM_STRING("hexversion",
PyInt_FromLong(PY_VERSION_HEX));
svnversion_init();