1

I'm just getting started with Python and am trying to run a program from the command line, as it is done on this website under the heading "Python Program". So I made script hello.py, it's located in my computer at C:\Python27.

In the example, they run the script by typing python hello.py Guido. When I try to do this, it doesn't work. Firstly, I'm not entirely sure what is meant by the 'command line', but I'm using cmd.exe in Windows XP. I get this:

python: can't open file 'hello.py': [Errno 2] No such file or directory.

I have already specified PATH as C:\Python27.

Also, when I try to run the program from the Python shell by typing hello.py Guido I get

SyntaxError: invalid syntax.

5 Answers 5

3

When you start cmd.exe, the default directory is your Documents and Settings: since your file hello.py is not there, the python interpreter can't find it, thus giving you the [Errno 2] No such file or directory error. To solve that, just change your current working directory:

C:\Documents...>cd C:\Python27
C:\Python27> python hello.py Guido

Anyway, it is a good approach not to having your files inside the python directory (create a directory in your documents for python sources and use the same approach).

When you are running the python shell, you cannot explicitly call python files, so in your case it tries to run hello.py as a command (which doesn't exists) and it gives you a syntax error.

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

1 Comment

Thanks a lot! That was really clear. I understand it much better now.
3

You need to locate your cmd current directory at C:\Python27:

cd C:\Python27

because the path python loads is relative. You can also use a full path:

python C:\Python2.7\hello.py

2 Comments

Typing the full path works, thanks! And sorry for not understanding, but how do I locate my cmd current directory at C:\Python27? Do I have to make a new system variable?
Oh I see, I just type cd C:\Python27 in the command line :)
1

Try without "python", when you put python directory in path, it automatically connects ".py" extension with python, so there is no need in writing "python hello.py Guido"

Just go to directory where .py is located, and call "hello.py"

5 Comments

The strange thing is, you are right that it works when I type hello.py Guido, but when I have already typed python in a previous command, now I suddenly get a syntax error for the same input?
It isnt strange, i dont know why i have so much minuses.
Syntax on google page is for linux, not for Windows, please post your python code.. It has something to do with your code, not with Windows (you got error 'Syntax error')
Thanks for your help Goran. I got it working in the meantime.
No problem :) Thanks for noticing that my answer wasn't wrong :)
0

What's your current working directory and where is hello.py located? To execute that command, hello.py should be in the same directory from where you started the commend line (cmd.exe). Otherwise you need to the write the absolute path of hello.py (like python C:.....\hello.py Guido) instead of just the filename 'hello.py'.

Comments

0

I had also this problem but because of ether reason: I accidently added spaces to the names of some of the file's names, so the CMD didn't recognized the names. for example: 'run .bat'

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review

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.