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

Terminal to Run Python and Initiate Class

I have a lot of trouble using workspaces, it seems very slow. I have been running my python out of atom and using terminal to call the python file. I am having trouble during the object oriented python to import and call classes through the terminal.

What I have tried:

Terminal Command (from root): Python3

Python Code

from 'desktop/learning python/object oriented python.characters.py' import Thief

ERROR

from 'desktop/learning python/object oriented python/characters.py' import Thief File "<stdin>", line 1 from 'desktop/learning python/object oriented python/characters.py' import Thief ^ SyntaxError: invalid syntax

==========

characters.py

class Thief: sneaky = True

1 Answer

jonlunsford
jonlunsford
16,746 Points

Kyle:

I believe what you need to do is navigate to the directory in Terminal where you have characters.py saved. For example in a Terminal window type:

$ cd desktop/learning\ python/object\ oriented\ python

this will make the 'object oriented python directory' your current directory. (NOTE: If you are unfamiliar with the "\" this is an escape character for the space in your directories.) From the current directory run python:

$python3

when you get to the Python REPL, then you should be able to do your import

>>> from characters import Thief

If you are running Python from the command-line, I've found it is always easier to run it from the current directory where your code is stored.