ShadyBears
ShadyBears

Reputation: 4185

How do I run my Python program that I wrote?

enter image description here

I am trying to run my celcius.py file but I don't know what to type in the command line...

When I type Python celcius.py it does not work:

I get the following message:

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

When I open it the program with the Python IDLE GUI it runs the program and closes the program before I can see it. I read somewhere that it won't close if you run it from the command line.

Upvotes: 1

Views: 15361

Answers (3)

fujy
fujy

Reputation: 5264

You don't need to do this, just open the command prompt and type:

python path/to/your/file.py

Just make sure that python command is set in your environment PATH, check this

Upvotes: 1

Ben Schwabe
Ben Schwabe

Reputation: 1289

There are a few ways of doing this.

WAY 1

step one: Open command prompt (go to start and type in "cmd") *NOTE: do NOT open python. if you see ">>>" in your command prompt window you opened python. This is bad. step two: type in this: python "path\to\file\celcius.py" replacing path\to\file with the path (starting with the name of the folder that you're computer user is named. So for my computer that would be ben. So let's say my celcius.py is located on the desktop I would run python "Desktop\celcius.py" and hit <.enter.>. It looks like you have celcius.py in your python folder. Don't put it there. that's the system files for python itself. move it to your desktop or your documents folder. By the way, if you have truble using this, make sure you're system is set up to execute the python command. Go here for reference.

WAY 2

assuming you have python associated with the .py file in explorer, you can find the file and double-click on it.

WAY 3

open IDLE (you can usually find it in your list of programs in start). Open the python file through IDLE. This will show you the source code for your program. hit <.F5.>. F5 will execute the program from within the the IDLE shell.

Upvotes: 1

dwarduk
dwarduk

Reputation: 959

You need to type "python celsius.py" in the windows command prompt, rather than the Python command prompt; this only works straight away if the Python directory is in your PATH. It probably isn't, so you need to use this instead: C:\Python27\python.exe celsius.py.

Because celsius for you is in the same folder as python, you can do this as well:

CD C:\Python27
python.exe celsius.py

In general, change the directory (CD) to where your file is stored, then run C:\Python27\python.exe <filename>

Double-clicking is generally easier in Windows, though, assuming it is set up to do that (it normally is unless you change it back)

Upvotes: 2

Related Questions