I've got a script in python which looks exactly like this:
x = input("Enter your name: ")
print("Hello " + x)
input("Press<enter>")
I've saved it correctly, and when I open the .py file, the terminal opens, and then closes almost instantly. I've figured out it says SyntaxError: invalid syntax. I've checked my code and to me its correct? I'm new to Python and I'm also using Python 3.3.2, the latest version.
Why is this happening?
The Python 2.x function input() can only be used with integers.
3.x can be used with both strings and integers. You are probably using Python 2.x.
For Python2.x, you must use raw_input()
To get your code to work, you must use Python3.x
These are the steps that I would follow to run the file:
open Terminal (or Powershell if you're using Windows)
go to the directory in your terminal where the python file resides that you're trying to fun
run the python file using for Terminal: python filename.py
or for Powershell: filename.py
This should work for you. If you're double clicking the file and trying to get it to run that way then yes, it will show a Terminal pop up and close immediately. If you're trying to open it to edit it and you're using Windows, you need to right-click the file, and select "Edit with IDLE"
Before it says SyntaxError, you should also see something like File ..., line ...
which would give you the exact line where the error occurred.
Add the following lines in front of your code:
import sys
print(sys.version)
... here the rest
and launch the script the same way you did before. This will display what version of Python is really executed.
Related
Hi I am a beginner to Python. I am writing the following sample code for input but when I press Enter after entering value for input command, the program doesn't go forward after printing the first line. I've done it in both Atom and Sublime Text. A basic print statement works perfect.
print ("hello")
x = input("Enter value")
print(x)
Sublime does not support user input unless you install a third-party plugin. And Atom is no different.
If you like to input values interactively, I would suggest running your script from the app Python IDLE then go to file and open your Python script and run it.
Or, alternatively, open a CMD (if you are PC user) or Terminal (in case of MacOS) and execute your script directly from the command line interface, like below:
python yourscript.py
may be the problem is with the code editor. Try running the file using cmd if you are on windows or terminal on mac/linux.
python file_name.py
Path must be messed up and I can't fix it.
In the command prompt I am trying to open and run a python program that I made in IDLE. I am running Python 3.8.5. According to Automate the Boring Stuff, I should just be able to do:
py birthdays.py
But when I do that I get:
C:\Users\name\AppData\Local\Programs\Python\Python38-32\python.exe: can't open file 'birtdays.py': [Errno 2] No such file or directory
All of my scripts are located in: C:\Users\henri\AppData\Local\Programs\Python\Python38-32.
The only thing that I could think of is that it is going one step too far and searching in python.exe instead of just Python38-32. But Ii'm not even sure it works like that.
I tried to add the exact path using:
set PATH=%PATH%;C:\Users\name\AppData\Local\Programs\Python\Python38-32
But when I do this and press enter nothing happens.
when I want to use CMD to run python scripts, I just use
cd\
to back the main root of drive C. then use
cd folderAddress
for several time until to reach the exact scrip containing folder. Finally, I type
python scriptName.py
In your command prompt type python.The output should be python 3.** (depending on your python version).
If you see the python version it's working fine. In command prompt navigate to the folder that you have your python file using cmd command. then type
python birtdays.py
Don't forget the space after python.
I have downloaded Atom.io for window and done setup successfully, then I download the package for running codes. but the Python result always come with an error like the picture.
Can you help me on this issue? Thank you
Try typing in:
python myprogram.py
However, you will still receive an invalid syntax error because:
print(*hi*)
should be:
print('hi')
Also, if you want to enter the python terminal, type:
python
Then you can type these commands (i.e. print("hi")) directly into the terminal instead of creating a .py file and running it.
One final point, I use the following instructions to add the python to PATH: how to add python to path in windows.
You can check this Stack Overflow thread for more details.
However, if you just want to play around with Python in Windows:
Download Python and install it.
Go to Start > type "IDLE" and click "IDLE (Python)".
Start coding! For example, type print("Hello, world!") and press the Enter key.
My first contact with Python was IDLE and I liked it a lot. Have fun!
It looks like you're running this from powershell. You can tell it's powershell because it says "Windows Powershell" on the first line of the output. Here's what I would do if I were you:
To start the python shell type python and press enter.
1a. Enter what you want to run in python, print('test') for example
If you want to get out of the python shell, enter ctrl-z and press enter to return to powershell.
To run python scripts use the command python {script}.py
Installing python on path
If you tried the above steps and got an error to the effect of "I don't recognize python" you should ensure that python is correctly installed in your PATH.
You can follow these instructions to install python correctly.
I have a little issue using pygtk, i have this project written in python, now the program works correctly, i have already added the string for the execution using terminal:
#! /usr/bin/env python
Now i want to know how to exec this script hiding the terminal that pop out once you double click the file.
Anyone knows?
Try saving it as a .pyc and executing that one.
Take a look at this and this.
Basically this:
import py_compile
py_compile.compile('abc.py')
I'm trying to learn python but have some problem running source files from power shell. When I type 'python' it opens up and I can type python commands directly in the shell. I think this is called interactive mode. But when I try to run/execute a source file I get an error message: It sayys: Syntax error: invalid syntax.
I use 'python myfile.py' when I try to execute the script.
If I run the same file from IDLE it works just fine. Ana idea what I'm doing wrong?
Here is myfile.py. I'm running python 2.7
# filename: myfile.py
while True:
s = raw_input('Enter something: ')
if s == 'Quit':
break
print 'Lenght of the string is', len(s)
print 'Done'
You might have more than one version of Python installed and the version IDLE is using is newer. To see what version of python you have you can type >python -V at a command line. If that version looks appropriate then you might need the full path to the file as the second parameter. E.g >python C:\myfile.py.
If you installed Python correctly there is always a chance that just typing the name of the script will run it with python. E.g. >myfile.py
I always find that adding C:\Python27 to the %PATH% variable and .PY to the %PATHEXT% variable makes running scripts easier. In this case just >myfile should work.
Edit after Update:
Typing just >python with no parameters opens python in 'interactive mode' which is different from the batch or scripting mode that your script is intended for. If executed with arguments the first argument is taken as the file path and further arguments are passed to the script in the sys.argv list.
You will need to put the full path of the Python executable within the command line in order for it to work. You could check and ensure that your python exe is included in your Path among your system variables.
Disclaimer: I don't know PowerShell, but I do know cmd.exe.
I don't know why python myfile.py doesn't work, but assuming that PowerShell bears at least some similarity to cmd.exe, the following should probably work: myfile.py. That's right, just enter the name of the Python script and hit enter.
If you started by typing "python" in powershell you will need to get out of that script.
If you are in python type:
quit()
then type
python myfile.py
This should work if your python is installed correctly.
Try to type this in Powershell:
$env:path="$env:Path;C:\Python33
After this, command
python yourfile.py
should work.
This my sound silly, especially coming from a beginner.
Just save the file on your desktop. Open up powershell and drag the file directly into powershell and it opens. kind of tedious but it works