0

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.

4
  • Have you tried adding it as an alias in the bashrc file? I would do that to allow it to run as ./scriptName Commented Oct 13, 2014 at 19:12
  • Have you make the script executable? (chmod +x testif.py) Commented Oct 13, 2014 at 19:16
  • this works, but I need to do this every time I want to type ./ .Is there a way that I can figure it out at one-time? Commented Oct 13, 2014 at 19:40
  • @XiaomingWu: if you don't want to make each script executable (like chmod +x testif.py -- note: you only need to do it once per script) then run it as python testif.py or python -mtestif if testif.py's directory is in sys.path (the current directory is). Commented Oct 13, 2014 at 19:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Is there any difference between python3 scriptname.py and ./scriptname? If I only write python scriptname.py, does it mean that it use python 2.7.5 not python 3.4.1?
@XiaomingWu you need to include the full name of the file, including the .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.
@J.F.Sebastian that's true, but I doubt OP is using virtualenvs. On Ubuntu, /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.
@MattDMo: we don't know who setup OP's computer. OP might be unaware that python command belongs to a virtualenv. It is misleading to claim that python means python2.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.