I am a beginner of python, and here's a problem concerns me all the time. For example, a really simple code:
a = int (input (' please a number '))
if a <0:
a = 0
print ('change to zero')
elif a == 0:
print ('zero')
elif a == 1:
print ('one')
else:
print ('more')
This code runs well in terminal when I type in python testif.py (the file name). But when I try to type ./testif.py It shows:
-bash: ./testif.py: Permission denied
This also happen in ubuntu, I tried to add
#!/usr/bin/python
at the first line, it still doesn't work
So please help me with my problem, struggle me all the time.
1 Answer 1
If you want to be able to run a script from the command line (./scriptname.py) without having to type python3 scriptname.py, then you need to make the file executable using chmod +x filename.py, and add a shebang (#!/usr/bin/env python3) as the very first line of the script. This needs to be done for every new script you write, but once it is done for a certain file it does not need to be done again for that same file. Alternatively, of course, you can just get used to writing python3 scriptname.py.
Just to be clear (from discussion in the comments) - while on standard vanilla Ubuntu systems /usr/bin/python should point to Python 2, just running python from the command line may invoke either Python 2 or Python 3, depending on how the environment is set up (non-system Python installed in /usr/local/bin, home directory, or elsewhere on PATH before /usr/bin, running virtualenv, customized symlinks, etc.). To be absolutely safe, follow J.F. Sebastian's advice: If your program will run without problems with both language versions, use #!/usr/bin/env python. If it is Py2-specific, use python2, and if it is Py3-specific, use python3.
8 Comments
.py, if present. If you add the python 3 shebang and make the file executable, then ./scriptname.py is the same as python3 scriptname.py (assuming you're using Python 3). python scriptname.py will run it using Python 2, python3 scriptname.py will run it with Python 3. Given that you're using input() in your example code above, you'll need to run it with Python 3 to get the desired results, as input() in Py2 means something completely different - use raw_input() in Py2, input() in Py3.python may refer to Python 3 too e.g., inside an active virtualenv. Though the default python on most (POSIX) systems refers to Python 2 interpreter. See The "python" Command on Unix-Like Systems, short version: use python shebang if your script is source-compatible with both Python 2 and 3, otherwise use python2 or python3 explicitly./usr/bin/python is Py2, and /usr/bin/python3 is Py3, at least as of 14.04, so I was trying to tailor the answer and comments to the OP's personal situation.python command belongs to a virtualenv. It is misleading to claim that python means python2.
chmod +x testif.py)chmod +x testif.py-- note: you only need to do it once per script) then run it aspython testif.pyorpython -mtestifif testif.py's directory is insys.path(the current directory is).