Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Running Python Files - Windows Terminal

Avatar image for fuelyou

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.

Avatar image for Philipp Acsany

Philipp Acsany RP Team on Feb. 24, 2023

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 print("Hello, World!")?

Avatar image for fuelyou

fuelyou on Feb. 25, 2023

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!

Avatar image for Philipp Acsany

Philipp Acsany RP Team on Feb. 27, 2023

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 🤗

Avatar image for tonypy

tonypy on March 2, 2023

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.