views:

455

answers:

7
+7  Q: 

Compiling Python

How can I compile and run a python file (*.py extension)?

+11  A: 

python yourfile.py

You have to have python installed first. It will automatically compile your file into a .pyc binary, and then run it for you. It will automatically recompile any time your file changes.

http://www.python.org/download/

Paul McMillan
+5  A: 

Python compiles its files to bytecode before executing them. That means you have to have a Python interpreter installed on the target machine.

If you don't want to install Python on the target machine use py2exe, py2app or something similar.

Georg
@gs: you should have referred him to the Python website http://docs.python.org/tutorial rather than talking about py2exe and py2app
Helen Neely
@Helen Neely, sometimes it's nice to get a specific answer quickly, instead of mining through a laborious tutorial. Currently, a net of 3 people have found this question useful; that should tell you something.
system PAUSE
+3  A: 

To add to Paul McMillan's answer, if you are on Windows and you have Python installed, then any files ending with the extension ".py" should be associated with the python executable, allowing you to run it like so:

> myfile.py

In *nix, you can begin the file with #!/usr/bin/python and run it like so:

$ ./myfile.py

In *nix systems, if the first two characters of a file are #! then it will execute the file with the specified executable, which I set here to be /usr/bin/python.

geowa4
In Windows, I also like to add .py to the PATHEXT environment variable, making .py files executable just like .exe (i.e. without typing the extension). You still need Python installed.
Jason R. Coombs
+1  A: 

On most Unix-like systems, you can use the shebang to tell the operating system which interpreter should be called. You simply put

#!/path/to/python

in the first line of your file, where of course you have to replace "/path/to/" with the path you have on your system. In most cases this would be "/usr/bin/python" or "/usr/local/bin/python". On unix systems you could also look for the path with

"#!usr/bin/env python"

or invoke the command

which python

to find the path. You can then run your program with the command

./yourprogram.py

If it tells you that you do not have permission to do so, you have to use the command

chmod a+x yourprogram.py
Lucas
+2  A: 

If you want to transform a python source file into a double-clickable .exe on windows, you can use py2exe, which can help you build an easy to distribute package.

TokenMacGuy
+3  A: 

Python is an interpreted language, so you don't need to compile it; just to run it. As it happens, the standard version of python will compile this to "bytecode", just like Java etc. does, and will save that (in .pyc files) and run it next time around, saving time, if you haven't updated the file since. If you've updated the file, it will be recompiled automatically.

You can also run python with a -O flag, which will generate .pyo files instead of .pyc. I'm not sure it makes much difference. If speed is important, use psyco.

And yes, on Unix (including Linux, BSD, and Mac OS X, or in a unix shell on windows) you can use a shebang line at the top of the file to make the file automatically run using python. On windows, the equivalent is to associate .py files with python.exe, and then make sure your PATHEXT environment variable includes ".PY" extensions.

However, for windows, you more likely want to write a gui program in python (possibly using PyQT4 and ERIC4) which has a .pyw file as its main script, and has .pyw associated with pythonw (which comes with python on windows). This will let you run python scripts on windows just like other GUI programs. For publishing and distribution, you probably want to compile to an executable file using something like py2exe, as others mentioned.

Lee B
A: 

If you just want to compile sources, without running them, you can do this

compileall.py <directory>

this command will compile python code in that directory recursively

compileall script is usually located in directory like

/usr/local/lib/python2.6

i.e. <prefix>/lib/python2.6 (or similar, depending on prefixes set a python configuration)

As Lulu suggests, you should make sure that resulting .pyc and .pyo files are executable by the users you care about.

compileall can also be used as a module

import compileall
compileall.compile_dir(path)
Evgeny