Python Using Databases in Python Our Diary App Doing Data Entry

Python Shebang not working in Workspaces

After adding the following to diary.py:

#!/usr/bin/env python3

... and running the following in the console of Workspaces:

chmod +x diary.py
./diary.py

... I get the following error:

: No such file or directory

I'm running a local copy of the Python script on my Windows machine with no problems...

Any ideas Kenneth Love?

So I thought I'd test this out on my Raspberry Pi to see what was going on, and I got the same error if I tried to run it with ./diary.py without first running it with python3 diary.py. Maybe it was complaining that the diary.db file didn't exist?

In any case, it still doesn't work in Workspaces. I even tried changing the owner from nobody:nogroup to treehouse:users with chown, no luck.

Same issue if I try and add the shebang to the students.py file...

4 Answers

Jim Hoskins
STAFF
Jim Hoskins
Treehouse Guest Teacher

Hi Iain Simmons

After a bit of head scratching, I think we know what is going on. It looks like the files in your workspace were created in Windows, and uploaded into the workspace. Whatever editor you used was set to use Windows style line endings (AKA "\r\n" or "CRLF"). This causes a problem when trying to run the files on linux, because the shebang line can't have a carriage return (the "\r" or CR in CRLF).

Workspaces itself is maintaining the Windows style line endings of your existing file, but if you create a new file in the workspace, and copy + paste into the new file, that will work.

This is a problem you are likely to encounter if you develop on Windows, and run your code on linux. You want to make sure any editor you use is set to use Unix style line endings.

I hope that helps. If you need help setting up your editor, post what you are using and I'm sure we can find the right settings.

Jim

More info http://askubuntu.com/questions/372672/what-could-cause-a-script-to-fail-to-find-python-when-it-has-usr-bin-env-pyt/372691#372691

Thanks Kenneth Love and Jim Hoskins!

Copying and pasting within Workspaces didn't work (I guess it's still holding on to those Windows style line endings!).

Running this command in the console fixed it:

sed -i 's/\r//g' diary.py

Alternatively, you guys could install the dos2unix package in Workspaces and then add the command to the teacher's notes...

Oh and for anyone else using Sublime Text, add this to your user settings to avoid this problem of copying and pasting Windows style line endings:

"default_line_ending": "unix"

Just tried Iain's command in the console and it worked. I've only been using Workspaces thus far.

I have the same problem though. I did this lesson on my Windows machine but have only used the workspace and I get the same error message. Luckily the command which Iain posted helped out :-)

Yeah, this impacted me as well and I only used the workspace editor. While I could run the script by passing it to the interpreter directly, I couldn't just "./" it without getting that error message, until after running the sed one-liner.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Strange. I did it during the course (as you saw) and just did it just now to make sure it still worked.

Any ideas, Jim Hoskins ?