I'm trying to learn Python and am trying to execute a python file in terminal. I'm using 2.7.3 python version on my OS X. I've changed the directory in terminal to where the file is located, but I'm getting an error in terminal:
>>> python ex1.py
File "<stdin>", line 1
python ex1.py
^
SyntaxError: invalid syntax
The ex1.py file contains:
print "Hello World!"
print "Hello Again"
print "I like typing this."
print "This is fun."
print 'Yay! Printing.'
print "I'd much rather you 'not'."
print 'I "said" do not touch this.'
Any ideas on how to fix this? Thx a bunch.
>>> python ex1.py
You are trying to run your script from within a python interpreter. You don't want to do that.
Instead, just run that command in a terminal, not in the interpreter
$ python ex1.py
If you are still in the interpreter, you can press ctrl+d to leave it and return to the 'normal' terminal
Exit python interpreter by typing exit() or press Crtl+Z
You will see the terminal now.
Type python file_name.py to run the code.
Happy coding!!!
Try type
exit()
return; then
python ex1.py
return
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()
Well I have this code:
#!/usr/bin/env python3
for i in range(15):
print(i, end=' ')
when I run it like this in command prompt:
prog1.py
I get:
C:\Users\Ja\Desktop>prog1.py
File "C:\Users\Ja\Desktop\prog1.py", line 4
print(i, end=' ')
^
SyntaxError: invalid syntax
but when I run it like this:
py prog1.py
it works fine. It's running python2.7.15 instead of python3.6.5 (I tested it using sys.version).
( It's the same for icon clicks )
You may be under the impression that Windows understands the "shebang line":
#!/usr/bin/env python3
It does not. All Windows knows is that .py files can be run by Python. So you need to update your file association, or run the correct version of Python explicitly, passing the path to your script as an argument.
When I manually run this command in Terminal, it executes, but through Python it gives the error that the directory is not available in Python packages.
I am using the following command
source ~/trytry/shell.sh
This is my test shell file:
#!/bin/sh
echo hello
when I executed " source ~/test.sh ", it will print hello at console.
This is my python code:
>>> import commands
>>> commands.getstatusoutput("source ~/test.sh")
(0, 'hello')
It works without any problem. So, would you please show your code?
What it looks like to me is that you have a shell script, and not a python file which would have the .py extension instead of .sh. The error may have to do with the fact that it isn't a python file you're trying to run.
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
I am doing the Python tutorial byte of python 120-1 pdf
Here are the instructions:
Start your choice of editor, enter the following program and save it
as helloworld.py
Example 3.2. Using a Source File
#!/usr/bin/python
# Filename : helloworld.py
print 'Hello World'
(Source file: code/helloworld.py) Run this program by opening a shell
(Linux terminal or DOS prompt) and entering the command python
helloworld.py. If you are using IDLE, use the menu Edit -> Run Script
or the keyboard shortcut Ctrl-F5. The output is as shown below.
Output
$ python helloworld.py
Hello World
I entered the program into text wrangler and saved it as helloworld.py
I then opened my terminal and entered python helloworld.py.
I received syntax error: invalid syntax
I then tried helloworld.py and also received syntax error: invalid syntax
Can anyone tell me where I went wrong?
You're using 2.x documentation, but are running 3.x. Either downgrade Python, or find more recent material.
#!/usr/bin/python
# Filename : helloworld.py
print('Hello World')