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!
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
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
Related
Using just notepad I typed out:
print ('hello world')
and saved it under ThisPC>windows(C:)>Users>me>Anaconda3 as:
first_program.py
Now I'm in Anaconda Prompt, I typed in Python and got back:
Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
But when I try to run the script I thought I made, by typing in (without quotes) "first_program.py" I get back an error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'first_program' is not defined
I have no clue what's causing it. So far I've tried changing the syntax of the script, like:
print 'hello world'
print "hello world"
print (hello world)
print ("hello world")
and other variations with the parentheses and keep getting the same error message.
Please help me understand what I'm doing wrong.
When you type in python in anaconda prompt, it launches the python interpreter. Any Python code written in this context while be read and executed.
When you type in "first_program.py" within the context of the interpreter, it will rightly raise a NameError as in the context of the interpreter, this reference does not exist.
However, if you were to simply type in the python command print('hello world') and hit enter, the interpreter will render the output correctly and print 'hello world' on your terminal.
To run the python script, simply open a terminal or Anaconda Prompt (navigate to the directory in which the python script resides) and run your script by typing either python first_program.py or first_program.py.
Assuming you're on the command line, and in the same directory as first_program.py
you can do:
python first_program.py
This will start python with your file as what you want it to run and then exit when it is complete.
Lets just start by saying I am a complete greenhorn to python and programming altogether but I really do want to learn so if you could try and use terms a beginner would understand that would help me a lot.
OK After getting incredibly frustrated googling how to use python in kali Linux couldn't run a file there either, I downloaded it on My Windows 10 OS then I made a basic script that looks like
#! /usr/bin/python
a = 122
b = 344
print a + b
very simple right. saved it as math.py and went the the cmd prompt (because wikki how told me to) then typed in the location of math py :
cd C:\Users\Mitchel\Documents
I read a question on here that told me to use cd to enter the location. python responded with :
File "<stdin>", line 1
cd C:\Users\Mitchel\Documents
^
SyntaxError: invalid syntax
so I decided to go to try going to the next step anyways and just typed
python math.py
and got the same error. I tried double clicking the file, I tried "open with" and clicking python. I want to know I can type code in notepad or notepad++ and test it before I start because its reall hard to actually write the code in the command prompt itself.
Those are commands to enter at your system's command prompt, but you are entering them in the Python interpreter. Most tutorials assume that you know how to get around your system's terminal. See below for a demonstration.
Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.
C:\Users\TigerhawkT3>cd
C:\Users\TigerhawkT3
C:\Users\TigerhawkT3>cd ..
C:\Users>py -c "print(1+2)"
3
C:\Users>py
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> cd
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'cd' is not defined
>>> cd ..
File "<stdin>", line 1
cd ..
^
SyntaxError: invalid syntax
>>> py -c "print(1+2)"
File "<stdin>", line 1
py -c "print(1+2)"
^
SyntaxError: invalid syntax
>>> print(1+2)
3
>>>
I am getting the following errors when trying to run a piece of python code:
import: unable to open X server `' # error/import.c/ImportImageCommand/366.
from: can't read /var/mail/datetime
./mixcloud.py: line 3: syntax error near unexpected token `('
./mixcloud.py: line 3: `now = datetime.now()'
The code:
import requests
from datetime import datetime,date,timedelta
now = datetime.now()
I really lack to see a problem. Is this something that my server is just having a problem with and not the code itself?
those are errors from your command shell. you are running code through the shell, not python.
try from a python interpreter ;)
$ python
Python 2.7.5+ (default, Sep 19 2013, 13:48:49)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> from datetime import datetime,date,timedelta
>>>
>>> now = datetime.now()
>>>
if you are using a script, you may invoke directly with python:
$ python mixcloud.py
otherwise, ensure it starts with the proper shebang line:
#!/usr/bin/env python
... and then you can invoke it by name alone (assuming it is marked as executable):
$ ./mixcloud.py
Check whether your #! line is in the first line of your python file. I got this error because I put this line into the second line of the file.
you can add the following line in the top of your python script
#!/usr/bin/env python3
I got this error when I tried to run my python script on docker with docker run.
Make sure in this case that you set the entry point is set correctly:
--entrypoint /usr/bin/python
So after almost 2 days of not being able to get GLEW working: trying Gl3w.
The python script is complaining about Syntax Errors, said all the prints signaled a syntax error, so I removed them all. Now it's complaining about the imports... (basically it seems it's complaining of the entire code)
I don't know Python, but I can't find support for Gl3w... should I keep trying to "correct the script", which I think should be working fine? What am I "doing wrong"?
Thanks!
This is the script: https://github.com/skaslev/gl3w
You are using Python 3, while the script requires Python 2:
Its main part is a simple gl3w_gen.py Python 2.6 script
In python 2, print is a statement, in 3, it's a function. This difference leads to syntax errors when trying to run python 2 code with python 3:
$ python3.3
Python 3.3.0 (default, Sep 29 2012, 08:16:08)
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'Hello world!'
File "<stdin>", line 1
print 'Hello world!'
^
SyntaxError: invalid syntax
I am in the python command line (using python 2.7), and trying to run a Python script. My operating system is Windows 7. I have set my directory to the folder containing all of my scripts, using:
os.chdir("location").
os.getcwd() returns this location.
When I type:
python myscript.py
I get this error:
File "<stdin>", line 1
python myscript.py
^
SyntaxError: invalid syntax.
What have I done wrong?
The first uncommented line of the script I'm trying to run:
from game import GameStateData
It sounds like you're trying to run your script from within Python. That's not how it works. If you want to run myscript.py, you need to do it from a system command prompt, not from inside the Python interpreter. (For instance, by choosing "Command Prompt" from your start menu. I think it's usually under "Accessories" or something like that.) From there you'll need to change to the directory where your scripts are by using the CD command.
Based on the additional information you have provided it does look like you are issuing the command inside of Python.
EDIT: Maybe part of the confusion comes from the term command line. You are at the command line in both the "Windows command" shell, and also when you are inside the "Python shell".
This is what I get in the command line when inside the Python shell:
D:\Users\blabla \Desktop>python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> python testit.py
File "<stdin>", line 1
python testit.py
^
SyntaxError: invalid syntax
>>>
Or this:
>>> os.chdir("..")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>
My suggestion would be to open a Windows command shell window with the cmd command and then issue your python myscript.py from there.
For more help it would be helpful to see your code, at least the first few lines where the error occurs and some certainty as to where the python command is being issued.
As the other answers indicate, you are probably in the Python shell unintentionally. But if you really do want to run your script from there, try execfile("myscript.py")
on windows shell run echo %PATH%, and check if your .py is under any of the paths.