0

content of the file -

print "Go"

used chmod +x devansh.py to make executable the file

output:

*Error: no such file "Go"*

The file does run but error is always displayed in the output just like above.

Jacob Vlijm
  • 83,767
Dev
  • 3
  • You need to include the full content of the script, the full command you run it with. Too many open variables to answer the question as it is. – Jacob Vlijm Sep 22 '18 at 10:50

2 Answers2

3

Clearly the file devansh.py lacks a shebang line that indicates how the script should be executed. See Why does Python in Linux require the line #!/usr/bin/python? for an explanation of a shebang line.

Wrong

Content of devansh.py:

print "Go"

Attempt to execute it:

chmod +x devansh.py
./devansh.py
Error: no such file "Go"

Right

Content of devansh.py:

#!/usr/bin/env python

print "Go"

Execute it:

chmod +x devansh.py
./devansh.py
Go
PerlDuck
  • 13,335
2

Your question contains three different instances of bad Python usage.

  1. You're trying to run print "Go" as a bash command.
  2. It's not necessary to make a separate file to run a one-line Python command.
  3. The unnecessary separate file which you named devansh.py does not need to be made executable with chmod +x devansh.py.

I highlighted these three things in bold text so that you can easily find them.


You got the following error message:

Error: no such file "Go".

...because you're trying to run print "Go" directly from the terminal as a bash command instead of running it as Python 2.x code which is what it is.

To show the correct path to devansh.py drag the devansh.py file into the terminal. Then change directories using cd to the directory containing devansh.py and run the command to make devansh.py executable: chmod +x devansh.py again.

In order for the print "Go" code to execute successfully Python 2.x needs to be installed.

sudo apt install python2.7   

Then execute the devansh.py file by running the following command:

python devansh.py  

devansh.py doesn't need to have executable permissions. python devansh.py will run successfully even if devansh.py is not made executable.

Alternatively you don't need to install python2.7 if you change the print "Go" code to be compatible with Python 3 which is installed by default.

print("Go")

You don't need to have a file named devansh.py to run this code. Just type python to start the python interpreter. When the python interpreter is started the prompt changes to >>>. Then you can run the code directly in the terminal by typing print "Go" after the python prompt.

karel
  • 114,770