0

okay, im a new guy at all this, just randomly picked it up with my neighbor and we are both stuck at this. We have been following this tutorial(here) and have made it to 6.6 in the tutorial. I have searched the forums looking for a way to get passed my problem but all the of questions people have are too complex for me as of right now. I am running windows 8.1 on my laptop, i have python27. So here we go i put in,

>>> cd c:\\py

and i get

File "<stdin>", line 1
cd c:\\py
   ^
SyntaxError: invalid syntax

Then i searched around and found a thread saying to use os.chdir so i gave that a shot and got;

>>> os.chdir("c:\\py")
>>> os.getcwd()
'C:\\py'
>>>

So my guess is that it worked? so then i go ahead and try to run my program like it says to do, so i put in

python hello.py

and i get this in return

>>> python hello.py
File "<stdin>", line 1
python hello.py
           ^
SyntaxError: invalid syntax

I'm literally stuck, i have no clue what to do now. If someone can help me through this i will love you long time. Thank you

2 Answers 2

2

First of all, Python shell differs from system shell (cmd.exe). You try to run python script.py in Python interpreter instead of cmd.exe.

Open cmd.exe and type in python script.py to solve this. It'll run fine if it doesn't contain any errors. cd c:\\ doesn't work due to the same reason.

First quit() or exit() the Python interpreter (type one of them right in it) then type the commands you want to execute (such as cd) into terminal.

If you want to run code.py in Python interpreter, you can os.chdir("...") to the directory where your script resides and type import code. That may not work if your script contains

if __name__=="__main__":

All in all, Python interpreter is for running Python code right in it and command prompt (terminal, cmd.exe) is for running other non-GUI programs and much more.

Sign up to request clarification or add additional context in comments.

3 Comments

so, going cmd and typing python and making my cmd python is basically a live sketchpad to put in codes and see if it works? and if they do i put them into a file and run the file just through the cmd with python in front?
@akaphobia, you're right. Python interpreter is like IDLE without GUI.
@akaphobia, you can just run script.py, which will run using the .py file type association.
1

You are in the python interpreter which is an interactive shell. You can consider it "scratch paper" to test out or try different things.

To run your script : quit() in the command prompt run python.exe hello.py ( on windows.. on *nix just python)

4 Comments

okay so i got it to open in the cmd, but why wont it open when i have the cmd in python?
cmd isn't a recognized function call or object in python. you would need to do something like os.system('cmd .. ') or something of that nature. The interpreter can only recognize python.
i ment i got it to open in the command prompt but i was asking why it wouldnt open when i had the command prompt set to python.
Right, I understand :) So, the command prompt (when you type cmd and the black window pops up) is a command line shell that lets you interact with the operating system via command line .. commands. When you type python, that opens up a new program. The same as opening up the command prompt, its a completely different program, just in the same window. It is no longer the dos shell, so none of those commands work anymore, only python. Does that make it a bit clearer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.