Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSahar Nasiri
7,454 Points./diary.py
When I run ./diary.py, I get "No such file or directory" error! What is the problem?
7 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsRunning ./diary.py
looks in the current directory for the file diary.py
. You can list the directory using ls
to see if it is present.
Sahar Nasiri
7,454 PointsI did all these before and diary.p is an executable file and I runs when I write python diary.py, but still I cannot run it with ./diary.py command! :(
Chris Freeman
Treehouse Moderator 68,441 PointsOk a few more questions:
- if you're using workspaces, can you make a snapshot (icon upper right), and post the link to it?
- If local, which OS and command shell are you using?
Sahar Nasiri
7,454 PointsThanks for helping me, here's my code: https://w.trhou.se/94920izk35
Sahar Nasiri
7,454 PointsChris Freeman Thanks, it worked with diary2.py. But When I update the python path above diary.py I still get bash: ./diary.py: /usr/local/pyenv/shims/python3^M: bad interpreter: No such file or directory this error! Why can't I fix diary.py?
Chris Freeman
Treehouse Moderator 68,441 PointsIt looks like it doesn't like something in the shebang line. Can you post the forest few lines of the diary file? Plus also post the output of the command "which python3
".
Sahar Nasiri
7,454 PointsWhen I type which python I get : /usr/local/pyenv/shims/python
Here's diary code:
#!/usr/local/pyenv/shims/python3
import datetime
from collections import OrderedDict
import os
import sys
from peewee import *
db = SqliteDatabase('diary.db')
class Entry(Model):
content = TextField()#char field has to have length but we want this to be huge
timestamp = DateTimeField(default=datetime.datetime.now) # If whe wrote now() we would get the fisrt time that the script had been ran
class Meta:
database = db
def initialize():
""" Create the database and the table if they don't exist"""
db.connect()
db.create_tables([Entry], safe=True)
def clear():
os.system('cls' if os.name == 'nt' else 'clear') # If we are on windows it calls 'cls' and if we are on Mac or Linux it calls 'clear'.
def menu_loop():
"""Show the menu"""
choice = None
while choice != 'q':
clear()
print("Enter 'q' to quit.")
for key, value in menu.items():
print('{}) {}'.format(key, value.__doc__))
choice = input('Actions: ').lower().strip()
if choice in menu:
clear()
menu[choice]()
def add_entry():
"""Add an entry"""
print("Enter your entry. Press ctrl+d when you finished.")
data = sys.stdin.read().strip()
if data:
if input('Save entry?[Yn]').lower() != 'n':
Entry.create(content=data)
print('Saved Successfully!')
def view_entries(search_quary=None):
"""View previous entries"""
entries = Entry.select().order_by(Entry.timestamp.desc())
if search_quary:
entries = entries.where(Entry.content.contains(search_quary))
for entry in entries:
timestamp = entry.timestamp.strftime('%A %B %d, %Y %I:%M%p')
clear()
print(timestamp)
print('='*len(timestamp))
print(entry.content)
print('\n\n'+'='*len(timestamp))
print('N) next entry')
print('d) delete entry')
print('q) return to main menu')
next_action = input('Action: [Ndq]').lower().strip()
if next_action == 'q':
break
elif next_action == 'd':
delete_entry(entry)
def search_entries():
"""search entries for a string"""
view_entries(input('Search_quary: '))
def delete_entry(entry):
"""Delete an entry"""
if input('Are you sure? [yN]').lower().strip() == 'y':
entry.delete_instance()
print('Entry deleted!')
menu = OrderedDict([
('a', add_entry),
('v', view_entries),
('s', search_entries)
])
if __name__ == '__main__':
initialize()
menu_loop()
Chris Freeman
Treehouse Moderator 68,441 PointsSorry didn't see your comment at the top. And what is the output of the command "which python3".
Chris Freeman
Treehouse Moderator 68,441 PointsOK. Workspace environments change slightly over time. You now have the solution. Make the shebang match the output of the which
command by remove the tailing "3
"
Chris Freeman
Treehouse Moderator 68,441 PointsWhy diary.py
is different from diary2.py
is confusing. There must be something else different. But if adjusting the shebang works, call it good!
Sahar Nasiri
7,454 Points:( I did remove the "3" but still get this: bash: ./diary.py: /usr/local/pyenv/shims/python^M: bad interpreter: No such file or directory
Chris Freeman
Treehouse Moderator 68,441 PointsThis is very strange non-deterministic behavior (very uncomputer like). If diary2.py
runs, but not diary.py
and there is no other differences between the two (check by running diff diary.py diary2.py
), then I suggest overwriting diary.py
with the working diary2.py
using $ mv diary2.py diary.py
.
Sahar Nasiri
7,454 PointsFinally it fixed :))! Thanks alot :))
Sahar Nasiri
7,454 PointsSahar Nasiri
7,454 PointsI did and there is an executable file named diary.py, but I still get the same error when I run ./diary.py!
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsOk. It could be a few other reasons. Try
python daisy.py
Or
Verify the file is executable. Try
ls -l
to verify the file is executable. The output should show an "X" in the listing such as "rwx".
Check that the file has the correct shebang path "
#!
" as the first line.Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsHey Sahar Nasiri, I think I have it solved. In short, the shebang path is incorrect.
When running
./diary.py
the shebang value at the top of the file is executed. In this case, the value in line 1 incorrectly points to a non-existent location for python3. Correcting this path allows execution:Alex Crump-Haill
7,819 PointsAlex Crump-Haill
7,819 PointsHi Chris, can you explain what calling 'ls' does, where it comes from and why it is useful? Thanks
Anders Axelsen
3,471 PointsAnders Axelsen
3,471 PointsHi there, Chris.
I have been struggling with this for some time now. I was wondering if there is a solution. :-)
My first shebang:
#!/usr/local/pyenv/shims/python3
My second shebang (formatted as in video):
#!/usr/local/pyenv/shims python3
Permission denied
and was led to believe achmod +x diary2.py
would solve the issue. It didn't happen.Snapshot: https://w.trhou.se/ogiefaq98o
Commands from console:
boi
14,242 Pointsboi
14,242 PointsHey Chris, you solved my issue flawlessly, THX. I have a question tho, where did you learn these workspace commands? Like
echo -n '#!' > diary2.py
for example. Could you share that with me?Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 Pointsecho
is a βbash commandβ. Many references can be found using a web search. Hereβs one https://www.educative.io/blog/bash-shell-command-cheat-sheetboi
14,242 Pointsboi
14,242 PointsWow! a quick response. Do you live in the internet? π. Chris, that information was brilliant. Can't appreciate you enough.