2

I have been working with Python for a short while and have created some programs, Im just setting up a third Raspberry Pi with Raspian Jessie. On the other two Pis I didn't run into an issue, but on this one I can't get my Python script to execute the same way.

The first line of the file has the shebang:

#!/usr/bin/env python

I git cloned the repo and went to the directory and did:

chmod +x script.py 

but when I try to ./script.py it doesnt work. From the Desktop it asks if the file should be executed and when I choose execute in terminal it closes and does not execute. I tried placing script.py in

/usr/local/bin

but that has no effect. If I do:

python script.py

it works fine.

The only difference between the Pis is the first two are headless and this new one has the Pixel desktop environment.

7
  • What do you see if you run /usr/bin/env python -V in your terminal? Commented Feb 10, 2017 at 17:51
  • I see Python 2.7.9 Commented Feb 10, 2017 at 17:53
  • Well, that means that env is correctly locating your python executable. Commented Feb 10, 2017 at 17:54
  • What exactly does "doesn't work" mean? When you run ./script.py, nothing happens? Do you not see any output or errors? Commented Feb 10, 2017 at 17:54
  • I get : No such file or directory Commented Feb 10, 2017 at 17:56

1 Answer 1

3

You probably made the file on a Windows PC and it got saved with DOS-style line endings. The linux shell (bash) doesn't like that and gets confused when trying to execute the script file.

Try to convert the file to UNIX line endings:

dos2unix script.py

You may need to do apt-get install dos2unix.

As a tip, you can use an editor/IDE in Windows that supports/saves as UNIX-style line endings to avoid this.

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

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.