2

I have a script/app/program written in python 3. I uploaded it to my Ubuntu box and changed the permission to allow execution by all. I am able to run python myapp.py with no problem but I cannot run myapp.py. I get an error that it is not a recognized command. I have at the top

#!/usr/bin/env python3

That should be right from all that I've read so far. I even tried

#!/usr/bin/python3

in the program referred to as myapp.py Neither of them work. I was following an online course and all was going well until we got to that point of running python scripts like regular programs by setting the execute setting.

3
  • can you please run 'which python' from unix shell and 'which python3' and update the question with result..also what error it gives when you run myapp.py Commented Dec 26, 2013 at 12:45
  • which python gives /usr/bin/python and which python3 gives /usr/bin/python3 Commented Dec 29, 2013 at 21:52
  • error is : No such file or directory. I did ./words.py from within that directory that has the words.py program. I've tried it with two versions of the shebang line #!/usr/bin/python3 and #!/usr/bin/env python3 which should have worked. Commented Dec 29, 2013 at 21:55

7 Answers 7

1

If you are talking about, executing it from any directory, you need to do two things.

  1. Setting the path variable. Lets say I need to execute Test.py, which is in Desktop, from any directory

    export PATH=$PATH:/home/thefourtheye/Desktop/
    
  2. Giving execute permission to the file

    chmod 755 /home/thefourtheye/Desktop/Test.py
    

Then I can execute it by simply typing Test.py.

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

1 Comment

I wish I could accept more than one response as more than one of you were helpful in solving the problem. Anyway, the error, it seems was that I had not put a space between the shebang and the /usr/bin/env python3. I seem to have read somewhere that one should not put a space in there. Upon adding that space after the shebang it worked as it should. What a frustrating error in my code!!
1

You cannot execute file in unix by name without directory name due to some security considerations, so you have to add . as directory (it will look like ./myapp.py)

Comments

0

. isn't on your path (and it probably shouldn't be for security reasons), so Ubuntu won't look in the current directory for a myapp.py program to run. You need to explicitly specify ./myapp.py to indicate that you want to run the myapp.py file in the current directory.

1 Comment

I am doing that now and still getting the same errors : No such file or directory. I actually thought it was due to the required command line argument being missing but when I supplied that it gave the same error. If I do python3 ./words.py arg1 it works fine.
0

You should either call it as

./myapp.py

or the current directory should be in the PATH environment variable (either as full directory path name or as '.' which indicates the dynamic current path), check with

echo $PATH

to add it you can run

export PATH=$PATH:.

1 Comment

I tried that, adding the current folder to the path and it still didn't work. Still getting : No such file or directory.
0

If you want to run it like:

user@machine:~$ myapp.py 

You must put the script in the /usr/bin/ or /bin/ or something similar. In other case you must run like:

user@machine:~/appFolder/$ ./myapp.py 

1 Comment

The same of other answers
0

did you try:

$ sudo chmod a+x myapp.py

then run the python code using:

$ ./myapp.py

1 Comment

it works on my machine. make sure that you are on the directory of myapp.py to execute ./myapp.py
0

For linux users, may be text format dos/unix problem!

try,

sudo dos2unix name_of_your_script

then shebangs will probably get to work properly! That was what happened to me.

PS: Ofcourse, also your script has to be executable (sudo chmod +x name_of_your_script )

Comments

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.