I have some Python files that I wrote on my Windows PC and then sent over to a Linux machine using pscp. They work as intended on Windows, but when I try to execute them on the Linux machine, I always get an error message.

I use the following process to run the code after importing the file "test.py":

First, I add the shebang line at the top of test.py:


# rest of code

Then I modify permissions and try to run the script:

chmod +x test.py

which gives me the error message

./test.py: Command not found.

If I create a file from scratch on the Linux machine and type in the exact same program text, the program will run just fine. Also, if I type the following code, the program runs just fine:

python test.py

Can anyone explain what is happening here? Thanks.

  • 6
    If you're coding on windows, your files may well have windows line endings. Then the shebang line would be #!/usr/bin/python\r and that command cannot be found. Try dos2unix – glenn jackman Mar 26 '15 at 21:35
  • Used dos2unix, it worked perfectly. Thanks glenn jackman. – CloudyGloudy Mar 26 '15 at 21:54