I am running a basic Python file on Windows XP from IDLE.
The file name is assignment1.py.
The file content is:
import sys
var = 5
but when I run it, it gives the error:
Command: python assignment1.py
SyntaxError: invalid syntax
Then I tried another thing which also gave an error:
Command: which python
SyntaxError: invalid syntax
Not sure if the installation is wrong or something.
I am able to run the print command successfully:
>>> print "I am working fine"
I am working fine
Not sure of the issue. Request help.
It looks like what you are entering as the "command" is being interpreted as Python code. I mean, python assignment1.py is interpreted as Python code. The result is as expected:
$ python -c 'python assignment1.py'
File "<string>", line 1
python assignment1.py
^
SyntaxError: invalid syntax
You need to run the file in the correct way, probably via the IDLE menu or by pressing F5. You can check these questions for details:
How do I run a Python program?
Running Python script from IDLE on Windows 7 64 bit
Related
This is the error, I have no idea where the fault is:
> & C:/Users/tanel/AppData/Local/Programs/Python/Python38/python.exe
"c:/Users/tanel/Documents/Projects/Coding/Python/Game Alien/game.py"
File "<stdin>", line 1
& C:/Users/tanel/AppData/Local/Programs/Python/Python38/python.exe "c:/Users/tanel/Documents/Projects/Coding/Python/Game Alien/game.py"
^
SyntaxError: invalid syntax
You're getting a Python SyntaxError because you are trying to run a PowerShell command in the Python shell:
>>> & C:/Users/tanel/AppData/Local/Programs/Python/Python38/python.exe "c:/Users/tanel/Documents/Projects/Coding/Python/Game Alien/game.py"
exit out of that shell and then run your PowerShell command in PowerShell. The prompt should look something like
PS C:\>
The Python shell is for running code interactively, e.g. you could type something like import pygame or print("Hello, world!") directly into it.
To solve this error, just write this command in your terminal :
>>> exit()
when i running code with pyspark in apache zeppelin 0.8.1 , i get error like
java.lang.NullPointerException
at org.apache.thrift.transport.TSocket.open(TSocket.java:170)
at org.apache.zeppelin.interpreter.remote.ClientFactory.create(ClientFactory.java:51)
...
so i open all file in my folder zeppelin
and i try to running file zeppelin_ipyspark.py at D:\zeppelin-0.8.1-bin-all\interpreter\spark\python with cmd
and i get error
D:\zeppelin-0.8.1-bin-all\interpreter\spark\python>zeppelin_ipyspark.py
File "D:\zeppelin-0.8.1-bin-all\interpreter\spark\python\zeppelin_ipyspark.py", line 29
port=${JVM_GATEWAY_PORT}, auth_token=gateway_secret, auto_convert=True))
^
SyntaxError: invalid syntax
so can you help me?
#LSS zeppelin_ipyspark.py is not a standalone script.
It is invoked internally from Zeppelin's java code as part of Ipython interpreter, where the python script is processed to replace these strings(like JVM_GATEWAY_PORT etc) with actual values.
You can check the code references: 1, 2
(New to Python)
I've downloaded a project from Github which contains an index.html file. When I bring the file to my web browser I get some errors that tell me I can't run js libraries.
In the Python shell, I'm trying to setup the HTTPSimpleServer using this command:
python -m SimpleHTTPServer
but I get this error:
File "<stdn>", line 1
python -m SimpleHTTPServer
^
SyntaxError: invalid syntax
I'm running Python 3.6.
I've also tried with
python3 -m http.server
and it produces the same error.
Am I missing a library? Don't see any syntax issues.
You need to type
python -m SimpleHTTPServer
at your command prompt, not inside the python interpreter. That is a python error message and the line above is a command line for your OS.
I am just a beginner in Python. I created a file called as cc.py and saved in the following path :
C:/Python33/cc.py.
I am trying to run this file but nothing is happening.
In the python shell I am typing Python cc.py but I am getting the following error:
SyntaxError: invalid syntax
I tried an alternative :
>>> execfile('cc.py');
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
execfile('cc.py');
NameError: name 'execfile' is not defined
The file contains the following lines of code :
import urllib
htmlfile = urllib.urlopen("http://google.com")
htmltext = htmlfile.read()
print htmltext
How should I run this file? I am totally confused. Can someone help me out?
print htmltext should be print(htmltext). Also, execfile() was removed from Python 3. It seems you are using a Python 2 book but running Python 3. These different versions of Python are incompatible, stick to one. For choosing which version, see this question.
An implemention of execfile():
def execfile(filename, *args, **kwargs):
with open(filename) as fp:
exec(fp.read(), *args, **kwargs)
In python 3, execfile no longer exists. You can open it and execute it manually:
def xfile(afile, globalz=None, localz=None):
with open(afile, "r") as fh:
exec(fh.read(), globalz, localz)
And to execute:
>>> xfile(r'C:\path\to\file\script.py')
Credit to: What is an alternative to execfile in Python 3?
That's how you execute files from the interpreter.
Another way, you can execute it from the command prompt. Just open it and type:
$ cd filepath
$ python file.py
About the script you're running, there's also a confusion. Whatever example you are following, it's a Python 2 example, yet you are using Python 3. Change the request line to this:
htmlfile = urllib.request.urlopen("http://google.com")
Hope this helps!
You wrote:
In the python shell I am typing Python cc.py but I am getting the following error:
SyntaxError: invalid syntax
If you want to run the python script, don't do it from the python shell. The "python" (not "Python") command needs to be run from a command prompt (DOS shell, terminal window, etc).
From a command prompt you should issue the command:
$ python cc.py
For a more complete description of the problem and solution, see Executing Scripts in the windows section of the python user guide, and also How do I run a python program under windows in the frequently asked question section of the python users guide.
import urllib.request
with urllib.request.urlopen("http://www.yourwebsiteurl.com") as url:
htmltext = url.read()
print (htmltext)
Am trying to run the following python program
import re
regex=re.compile("http...imgs.xkcd.com.comics.[\\S]*.[jpg|png]")
f=open('out.txt')
for a in f:
print regex.findall(a)
print '\n'
when I type the code into the interpreter manually, it works as expected
but when i save it as a file and try to run it , it gives errors.
The command i used to run it is
chmod +x
sudo ./pymod.py
ERROR:
./pymod.py: 2: Syntax error: "(" unexpected
if i dont use sudo, the error i get is
./pymod.py: line 2: syntax error near unexpected token `('
./pymod.py: line 2: `regex=re.compile("http...imgs.xkcd.com.comics.[\\S]*.[jpg|png]")'
am using ubuntu 10.04 with everything on default
it takes about 10-15 seconds for the error to appear
Your file should start with shebang. You should include the path to the python interpreter
#!/usr/bin/env python
import re
regex=re.compile("http...imgs.xkcd.com.comics.[\\S]*.[jpg|png]")
Check out : http://en.wikipedia.org/wiki/Shebang_(Unix)
This is probably executing as a bash script instead of in Python. Put
#!/usr/bin/env python
at the beginning of your script.
When you set something as executable, you have to specify what you want it to run it with, or Linux will consider it to be a bash script.
Add this as the first line of the file:
#!/usr/bin/python
Or run it like:
python pymod.py
Cheers!
Either use the "shebang". I.e. put
#! /usr/bin/python
as the first line of your script.
Or teach your ubuntu how to treat python scripts without it
as described here: http://www.daniweb.com/code/snippet241988.html