Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics Meet Python Input and Coding Style

Attempting to run hello.py with python hello.py will return a syntax error. Any way around this?

Hi guys I've pasted the code below:

python hello.py
File "<stdin>", line 1
python hello.py
^
SyntaxError: invalid syntax

I've tried this in an IDE and it displayed no issues.. Any ideas?

4 Answers

Steven Parker
Steven Parker
231,198 Points

That's just the command to start the program running, but the syntax error may be in the file contents. Also, where is it you pasted this? Are you using the workspaces? If so, you can make a "snapshot" and share the link to it here for a complete analysis.

https://w.trhou.se/9gck5f4ewi

Yeah I know, but the thing is when my file is as simple as (above link) there should be no syntax error. I've even tried removing everything and it still displays invalid syntax when I run an empty file.

Steven Parker
Steven Parker
231,198 Points

I created a fork (clone) of your workspace and it worked for me:

treehouse:~/workspace$ python hello.py
Hello, Michael
treehouse:~/workspace$               

As you can see, the command causes the Python system to start up, run your program, and then exit when done. But I can re-create the error messages shown above if I enter that command while already running python:

treehouse:~/workspace$ python
Python 3.6.4 (default, Oct  1 2018, 17:44:19)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> python hello.py
  File "<stdin>", line 1
    python hello.py
               ^
SyntaxError: invalid syntax

So just remember to only use that command from the command shell, and not from inside an interactive Python session.

Douglas Jackson
PLUS
Douglas Jackson
Courses Plus Student 174 Points

I just had the same problem in Workspaces. Turns out I missed the "Ctrl+D" step

Calvin Tiley
Calvin Tiley
7,492 Points

I had the same problem. I just restarted the workspace window and it worked fine. Must just be a bug.

Samet Erpik
Samet Erpik
1,187 Points

I've reloaded the workspace tab and it worked fine!