Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import twstock causes the script.pyw not run. #128

Open
seaspeak opened this issue Sep 10, 2024 · 8 comments
Open

import twstock causes the script.pyw not run. #128

seaspeak opened this issue Sep 10, 2024 · 8 comments

Comments

@seaspeak
Copy link

seaspeak commented Sep 10, 2024

Describe the bug
import twstock causes the script.pyw not run.

To Reproduce
Steps to reproduce the behavior:
Or you could use the script below "twstock_not_run.zip"

  1. Just incorporate import twstock in your script.
  2. Rename the script as script.pyw.
  3. Double click script.pyw
  4. If you comment the import twstock, then you could run the script by double-clicking.

Expected behavior
Run!

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • Windows 10, Python 3.7

Additional context

twstock_not_run.zip

@mlouielu
Copy link
Owner

It works on my Linux.

Please run it in your terminal and see the error message from it.

@mlouielu
Copy link
Owner

I suspect either you have multiple Python in your environment and the default one is not the one you installed twstock, or the tk init has some problem.

@seaspeak
Copy link
Author

seaspeak commented Sep 10, 2024

script.pyw should be Windows exclusive? Correct me if I am wrong. PYW is run by pythonw.exe so that we could get rid of the black command window.

I found the discussion here, but nothing works for me so far: https://stackoverflow.com/questions/24835155/pyw-and-pythonw-does-not-run-under-windows-7

My other PYW works fine and,
python script.pyw works (python script.py), but not pythonw script.pyw. I don't see any error messages dumped.

Simply try twstock_not_run.pyw on Windows, it works without the import twstock.

@mlouielu
Copy link
Owner

script.pyw should be Windows exclusive? Correct me if I am wrong. PYW is run by pythonw.exe so that we could get rid of the black command window.

That doesn't matter between platform.

I found the discussion here, but nothing works for me so far: https://stackoverflow.com/questions/24835155/pyw-and-pythonw-does-not-run-under-windows-7

As I said in the last reply, we should first figure out if there is any error message causing Python to exit early, and I suspect there has some problem of the enviroment or tk.

In order to figure that out, we need to get the terminal first, so that we can get the error message.

My other PYW works fine and, python script.pyw works (python script.py), but not pythonw script.pyw. I don't see any error messages dumped.

Because pythonw does not show the terminal, thus you can see any error message.

Simply try twstock_not_run.pyw on Windows, it works without the import twstock.

You can try to open up a terminal: https://learn.microsoft.com/en-us/windows/terminal/faq

And then run python twstock_.pyw, which should gives you the error message, or if it does not work, rename to .py and run it again.

@seaspeak
Copy link
Author

Let me rephrase the issue, it is Windows specific, pythonw.exe specific. You need to run pythonw twstock_.pyw on Windows to reproduce the issue. It was a failure to redirect the stderr. So I rewrite the twstock_.pyw to log the error.
twstock_.zip

  File "C:\Program Files\Python37\lib\site-packages\twstock\cli\best_four_point.py", line 9, in <module>
    getattr(sys.stdout, "buffer", sys.stdout), encoding="utf-8", errors="replace"
AttributeError: 'NoneType' object has no attribute 'readable'

Since the pythonw.exe doesn't have the std output, cli module raises the exception. I reassign the stdout to None. I didn't look into the code, will that impedes your code flow?

# XXX: Repalce sys.stdout prevent Windows UnicodeEncodeError on cmd.exe
try:
	stdout = io.TextIOWrapper(
        getattr(sys.stdout, "buffer", sys.stdout), encoding="utf-8", errors="replace"
	)
except AttributeError:
	stdout = None

@mlouielu
Copy link
Owner

Let me rephrase the issue, it is Windows specific, pythonw.exe specific. You need to run pythonw twstock_.pyw on Windows to reproduce the issue. It was a failure to redirect the stderr. So I rewrite the twstock_.pyw to log the error. twstock_.zip

  File "C:\Program Files\Python37\lib\site-packages\twstock\cli\best_four_point.py", line 9, in <module>
    getattr(sys.stdout, "buffer", sys.stdout), encoding="utf-8", errors="replace"
AttributeError: 'NoneType' object has no attribute 'readable'

Thank you, this is what we need to know in order to pin down the problem.

Can you try the branch https://github.com/mlouielu/twstock/tree/fix-pythonw-128 for the fix? You can clone it and copy the "twstock/twstock" directory to the same as your pyw file, or download the zip file, unzip, and copy the "twstock/twstock" directory to the same as your pyw file.

The directory of your pyw file should look like this:

foobar/
   |_ your.pyw
   |_ twstock/

Then Python should import the twstock with the fix. Please let me know the result, thanks!

@seaspeak
Copy link
Author

It looks fine.

@mlouielu
Copy link
Owner

My PR didn't pass the unit test, I'll try to fix it later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants