Compiling Python

Question:

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

Asked By: user161004

||

Answers:

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/

Answered By: Paul McMillan

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.

Answered By: Georg Schölly

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.

Answered By: geowa4

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
Answered By: Lucas

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.

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.

Answered By: Lee B

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)
Answered By: Evgeny

Answer for Windows

  1. first you must install python
  2. then set path variable
  3. after that write your python program and save
  4. think there is a python program that name “hello.py
  5. open cmd.exe
  6. then goto the path that you saved your “hello.py” file,
  7. and then type python hello.py and press enter key.

now the python code is automatically compile and show the result.

Answered By: ashanrupasinghe
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.