2

I'm getting ready to start a job (in C#.NET) where having some Python experience is a plus. I did some reading about it a year or two ago, and picked up another book (Python 3 Object Oriented Programming by Dusty Phillips). I'm at the first code example, and having a really stupid issue.

I created my first class in a separate file, first_class.py. It's saved in my C:\Docs\Continuing Education\Object Oriented Python\Chapter 2 folder:

class MyFirstClass:
    pass

He then says "run the command python -i first_class.py". I open up the ?Python 3.5 Console? (if that is what it's called), and put that in there. I wasn't quite expecting it to work because it's executing from a different folder than the file is in.

I tried executing the command again, with the fully-qualified location of the file with and without quotes, but neither of those worked. Seeing a few other answers around, I tried all three (without the folder, and with and without quotes) but omitting the .py extension, and that doesn't work. All of those commands so far have given the following error message with an error pointing to the end of the word "first_class":

SyntaxError: invalid syntax

I tried omitting the python part of the command since I'm already in the Python program, but that doesn't work either.

Next, I found this answer and changed the "current directory" to the folder I listed above. Same deal with and without the python and/or the .py.

I tried using the import function as well. That doesn't give any errors, so I thought it worked. I went on to the next command in the book:

>>>a = MyFirstClass()

Unfortunately, that gave the following error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>

NameError: name 'MyFirstClass' is not defined

I'm sure I'm just missing something ridiculously simple. If this is a duplicate, by all means, flag away.

Community
  • 1
  • 1
krillgar
  • 12,596
  • 6
  • 50
  • 86
  • If you imported `first_class` then you can access `first_class.MyFirstClass`. – Peter Wood Feb 09 '16 at 17:37
  • You need to run the command from a command line interface. On Windows its the Command Prompt. – Ryan Jackman Feb 09 '16 at 17:41
  • @PeterWood Excellent! That works! Thank you. But what is with the `python -i first_class`? I'll be happy to use the `from`/`import`, but would appreciate knowing at least the goal of that first command he gave me. – krillgar Feb 09 '16 at 17:41
  • @Fishstick I tried that, and it was telling me that python was not a valid command. (Sorry for forgetting that one) – krillgar Feb 09 '16 at 17:41
  • @krillgar You likely don't have python in your path variable. There are plenty of resources for editing your path online. – Ryan Jackman Feb 09 '16 at 17:43
  • I recommend you read the [python tutorial](https://docs.python.org/3/tutorial/). It'll take you a few hours, but is worth it if you try things out. – Peter Wood Feb 09 '16 at 17:43
  • @PeterWood Alright, thanks. If you think this question is worth saving for posterity, put your comment as an answer, and I'll accept it. Add the `from`/`import` part too for completeness. – krillgar Feb 09 '16 at 17:47
  • [This](http://stackoverflow.com/q/4383571/1195056) question is also a good way to achieve what I was trying to do, but only through the Python tools. – krillgar Feb 09 '16 at 20:20

1 Answers1

2

When the text says run python -i /path/to/file it's expecting you to run that from a shell, not from within the python console.

The -i flag is a way to pass a file or list of files to python that will run and then dump you into the interactive prompt. Any code in the file will be in the global namespace, as if you had typed it into the interactive console.

Open up a cmd shell (or powershell) and run:

python -i "C:\Docs\Continuing Education\Object Oriented Python\Chapter 2\first_class.py"

Then you should be able to call your class

a = MyFirstClass()
Brendan Abel
  • 35,343
  • 14
  • 88
  • 118
  • When I do that, I get the error `'python' is not recognized as an internal or external command, operable program or batch file.` Do I need to run that in cmd from the folder that the python interactive console lives in? – krillgar Feb 09 '16 at 17:54
  • `C:\python35` must not be in your system path. You'll have to provide the full path to the python interpreter. Instead of `python`, use `C:\python35\python.exe`, or wherever you installed python. – Brendan Abel Feb 09 '16 at 17:55
  • Alternatively, you could edit your system PATH variable to include the directory that `python.exe` is contained in. – Brendan Abel Feb 09 '16 at 17:57
  • OK, so best bet is to change directory in cmd to where python is, then execute. Thanks. – krillgar Feb 09 '16 at 17:58