Running Python Files
Okay, so now that we have this Python file with some Python code in it, let’s run it as a Python file. Okay, sure. Now I can just run it with the
python command because I have Python installed, and then I just need to put in the path, and I tab auto-complete.
One thing I didn’t mention is when you tab auto-complete, you might see the
.\ here. That’s just PowerShell being extra explicit about the path.
It’s saying—basically, that means the current directory, the
. and the
\, but you don’t actually need it. It will work without these two characters. So let’s run this …
Thanks for your kind words @fuelyou!
Then I tried to run the file but nothing happens. When I press enter, just a new line (current directory) appears.
Generally it’s a good sign “if nothing happens”. Well, at least better than getting an error.
Do you have code in your Python file that creates output. For example, a print function call like
Thank you for your advice @Philipp Acsany !
I ran the python file on text editor(sublime text) and failed. As you suggest, there is a problem in the file. The text editor tells me that the character code is not utf-8. I found it!
I suspect that my pc uses shift-JIS when I write the code from the terminal. It seems that I should make some change in my windows system.
Anyway, I’m glad that I was able to locate the problem. Thanks!
I’m happy that it works now, @fuelyou 🥳 In case you want to have more ideas for adjusting your Windows system, then you may check out the RP Windows Guide 🤗
The exercise with running hello_terminal.py in python using the basic Powershell installation failed on my PC (Windows 11, with Windows Terminal installed and runninh the default Powershell).
SyntaxError: Non-UTF-8 code starting with ‘\xff’ in file D:\Python\Real Python\pb_terminal\hello_terminal.py on line 1, but no encoding declared; see peps.python.org/pep-0263/ for details.
The file is actually encoded with UTF-16 LE BOM.
There are various workarounds flagged via Google search, none of which are pretty. There are short term workarounds in the terminal to set the Active code page e.g. 65001 (UTF-8) with the command chcp 65001 but I found that didn’t work. I changed the file to UTF-8 using Notepad ++ temporarily.
It seems that Powershell Core comes with the facility to set the encoding so I’ll probably use that for future work.
It would be useful to make a comment on this in the course.
Become a Member to join the conversation.
fuelyou on Feb. 23, 2023
Thank you for a wonderful course! As a windows user, I have been waiting for this kind of course.
Unfortunately I run into a trouble. I’m sorry for asking a stupid question but I need a help. I followed the tutorial and I was able to create a folder and a python file. Then I tried to run the file but nothing happens. When I press enter, just a new line (current directory) appears. When i use “python3” instead of “python”, then it prints only “Python”.
I try running python (by just entering “python”) in the same directory and it works as normal. So I just can’t run a python file directly from the terminal with a command “python hello_terminal.py”.
I want to know where I should look for the cause. Thanks.