[ACCEPTED]-Compiling Python-compiler-construction

Accepted answer
Score: 30

python yourfile.py

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

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

Score: 16

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

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

Score: 14

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

compileall.py <directory>

this command 8 will compile python code in that directory 7 recursively

compileall script is usually located in 6 directory like

/usr/local/lib/python2.6

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

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

compileall can also be 1 used as a module

import compileall
compileall.compile_dir(path)
Score: 8

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

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

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

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

Score: 6

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

> myfile.py

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

$ ./myfile.py

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

Score: 4

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

Score: 1

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

#!/path/to/python 

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

"#!usr/bin/env python" 

or 4 invoke the command

which python

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

./yourprogram.py

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

chmod a+x yourprogram.py
Score: 0

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 1 and show the result.

More Related questions