Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Odd Print Behavior
#1
I'm on Windows 10, running Python 3.12.3. I created a python script in a directory called 'python_alarm_help', and it contains print statements that correctly print to the command line when I call it with 'python alarm_file_reader.py'.

Later, I created a second script in the directory just above 'python_alarm_help' (this higher directory is called '***_python', don't want to disclose the full name, but it does contain the string 'python', in case that matters). This script was called 'data_handler.py'. It also had print statements in it, but when using 'python data_handler.py', the print statements did not output anything, not even a blank line. Also, I had input calls within this script that DO output and successfully take in input to the script even when I run the script inside the higher '***_python' directory.

I solved this print issue by copying 'data_handler.py' into 'python_alarm_help' and re-running with 'python data_handler.py'. The print statements work again. I'm curious why this happened? If it matters, 'alarm_file_reader.py' was the first python script I wrote on this computer.

I also tried a couple other sys related solutions in the higher directory, but no luck. I'm guessing there's some sort of cache and/or a configuration file I need to be aware of. Thoughts on this?
Reply
#2
Rename the file with the oh so secret name and see if that changes anything. It could be that importing the module in the higher directory (doesn't matter if it is in a higher directory) hides something used by print().
Reply
#3
Ha, yes oh so secret. I renamed the file and print statements now work! What's the lesson to learn here?
Reply
#4
I'm not sure what was learned. I'm still not sure what the problem was. Python doesn't really care where a file exists as long it is in the python path, and if the file is not in the path, you get a ModuleNotFound error, not odd print behavior.

It could be that ***_python.data_handler.py didn't have any print statements, and you added the print statements when copying code from that file to python_alarm_help.other_script.py. This is easy to do. You type in the editor but don't save the changes. Run the file and "Where are my print statements?!?"

I think the only lesson learned is next time you ask a question, condense the code to a short example that demonstrates the problem, and post that code to the forum. Don't try to describe the problem.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Question on `print f.read()` behavior pythonjohn 2 4,499 Nov-07-2016, 10:18 PM
Last Post: pythonjohn

Forum Jump:

User Panel Messages

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