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 trial

Python

Iain Watson
Iain Watson
3,031 Points

OOP Superclasses

I'm using Workspaces for the first time and I'm getting an error even though I tried to copy the instructor's code exactly: python: can't open file 'play.py': [Errno 2] No such file or directory.

I took a snapshot https://w.trhou.se/2jo7ztkt5j

Any help would be appreciated

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Can you cut and paste what you're seeing in the console window while trying to run play.py?

5 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The Python shell operates out of the current working directory (cwd) or folder where it was invoked. By running in the directory ~/workspaces, it can only see the sub-directory rpg.

The directory path in the prompt help remind you where you are in the directory hierarchy. To see play.py change the cwd to be "rpg" using the cd command.

treehouse:~/workspace$ python play.py                                                                        
python: can't open file 'play.py': [Errno 2] No such file or directory                                       
treehouse:~/workspace$ ls
rpg
treehouse:~/workspace$ cd rpg
treehouse:~/workspace/rpg$ ls
attributes.py  characters.py  play.py thieves.py
Iain Watson
Iain Watson
3,031 Points

Thanks Chris. I may have missed a Treehouse lesson on directories along the way.

Iain Watson
Iain Watson
3,031 Points

Chris, I am still struggling a bit with directories in Workspaces...is there something I can read? I got stuck in :/home$ for a while before figuring out how to get back to :~workspace$

Thanks,

Iain Watson

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

The tilde ~ is a short cut for the user's home directory. Using cd with no argument is the same as cd ~

Iain Watson
Iain Watson
3,031 Points

I did look at the Workspaces video but I think am asking about points not covered in the video. It would be helpful if I could read some documentation as I haven't come across Workspaces elsewhere.

Meanwhile, can you take the time to look at a snapshot of my current Workspace as the file I am currently trying to use in the Shell/console, protected.py, cannot be found when I try to import it. The link is:

http://w.trhou.se/863aw8t70t

I'm not sure if this is related, but this happened after I tried to clean up the left side of my Workspace as I had 2 folders with the same name and my nesting of files was all wrong.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

When looking at the last snapshot, I was able to run the following in the console:

treehouse:~/workspace$ cd rpg
treehouse:~/workspace/rpg$ ls 
attributes.py  characters.py  inventory.py  items.py  play.py  thieves.py  
treehouse:~/workspace/rpg$ python play.py 
Thief: Kenneth 
False 
True
False
treehouse:~/workspace/rpg$ 
Iain Watson
Iain Watson
3,031 Points

treehouse:~/workspace$ python play.py
python: can't open file 'play.py': [Errno 2] No such file or directory
treehouse:~/workspace$

I can see that the prompt line in the instructor's console shows the folder name rpg along with the $ sign. Why is that?

Perhaps I should also note that earlier I was using both tabs and spaces for indents and this threw an error. I'm pretty sure I fixed that.

Iain Watson
Iain Watson
3,031 Points

Chris, I appreciate your help.

I got play.py to run on my latest try.

I got ReversedStr() to work.

I now understand how to use cd name to go from treehouse:~ to treehouse;~/workspace$ to treehouse;~/workspace/rpg$ to treehouse;~/workspace/__pycache__$

However, when I run the very useful ls on rpg and __pycache__ it looks like I may have redundant copies of file lurking around.

Maybe you can look at http://w.trhou.se/863aw8t70t

It would still be helpful for me to read more to understand more

Thanks,

Iain

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

When you run a Python file, a compiled version is saved as a pyc. These compiled versions are run automatically if the source code has not changed. These pyc files used to be left in the same directory as the source py files. Now the pyc files are placed in the __pycache__ directory to keep them out of the way. This directory is typically not used as a working directory.

Iain Watson
Iain Watson
3,031 Points

Chris, can you look at my Workspace http://w.trhou.se/jc0ce0y59f ? I had 2 versions of scoresheets.py (one for the video originally residing outside the Yatzy folder, one for the Challenge originally residing inside the Yatzy folder). After I changed the name of the file used for the Challenge to scoresheets2.py and then switched the location of the 2 files, it appears they both became the same file. Curiously, while the directory on the left shows the 2 different filenames, clicking on either brings scoresheets2.py to the window on the right.

Also, is there a way to cut and paste the link to a Workspace snapshot? Currently I am writing it down and trying to be sure I type it correctly as above.

Thanks, Iain