0

I have been trying for the past two hours to understand what I'm doing wrong. I am just starting off in Python and I can't wrap my head around a few basic things.

I am using:
* Python 2.7.8
* Windows Powershell

This is my error:

>>> python ex1
  File "<stdin>", line 1
    python ex1
             ^
SyntaxError: invalid syntax

One thing to note, I've noticed if I force Python to start through a file, it unexpectedly closes. Could this be an administration issue?

I do not know how to do a traceback, or when I try it in Powershell, it gives me an error.

I fixed my silly error: I was using using Python in Powershell, instead of just using the command line. I also made an error in notepad++ - I accidentally had 2 spaces in Line 1 and 2 that I didn't catch.

Thank you for the help!

5
  • EX1 is the file i've created using notepad++ I am trying to run python by running what I have written in notepad ++ ^ Commented Aug 5, 2014 at 5:16
  • Run python ex1 from Powershell directly. Don't open up a Python session and then try to run python ex1 from there. Commented Aug 5, 2014 at 5:32
  • @user2357122 i get PS C:\Users\$\project> python ex1.py. File "ex1.py.", line 1 print "Hello World!" ^ IndentationError: unexpected indent PS C:\Users\$\project> Commented Aug 5, 2014 at 5:41
  • @Pcurry Hi, May I ask why the edit was needed? Some of these just seem to be personal things you dont like, rather than grammatical? Commented Aug 5, 2014 at 6:15
  • @Invention I capitalize the proper nouns in questions and answers, which I believe is correct grammar, as well as being more readable to me. Capitalizing 'I' is correct grammar, and so I fixed that. The rest of the layout changes I thought made it easier to read, and clearer what you were saying. If I have offended in my changes, I apologize. Commented Aug 6, 2014 at 8:27

2 Answers 2

3

You are trying to run ex1.py from python shell I think,

Because >>> is prompt for python shell. Please try to do that from command prompt.

I got same error when I tried from python shell.

root@localhost $ python
pythPython 2.7.3 (default, Feb 27 2014, 19:58:35)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> python ex1
  File "<stdin>", line 1
    python ex1
             ^
SyntaxError: invalid syntax
>>>

Please try

root@localhost $ python ex1.py
Sign up to request clarification or add additional context in comments.

5 Comments

Hi! I continue to get the same error. I am trying it in Windows powershell
SyntaxError: invalid syntax >>> root@localhost $ python ex1.py File "<stdin>", line 1 root@localhost $ python ex1.py ^ SyntaxError: invalid syntax
what is content in ex1.py?
maybe this will help, learnpythonthehardway.org/book/ex1.html This is where i'm getting it from. I am using this as a tutorial
Never mind! I found my error! I am a silly string of issues! I apologize. while trying to fix my error, I made a few others as well. in PowerShell I turned python on, instead of simply doing it in the command line and in notepad I accidentally put an space in line 1 and 2. that took 2 hours just for something as simple as that! Thank you for the help
1

I think you should try executing the file in cmd and not in REPL

I was getting the same error but now I tried doing the same on cmd it worked for me!

>>> notepad mybasic.py
  File "<stdin>", line 1
    notepad mybasic.py
            ^
SyntaxError: invalid syntax

Do not go inside REPL by typing python + enter rather than work on cmd.

C:\Users\Neha\IIEC_python>notepad mybasics.py

C:\Users\Neha\IIEC_python>python mybasics.py
Hii Neha

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.