How to Compile Python

A common question among programmers switching to or learning Python is how to compile their code. Many programmers are used to using Microsoft's Visual Studio, for instance, to write code, then compile it, creating an executable. Unlike lower-level languages such as C, however, Python is not compiled in a separate step before execution. Rather, when you execute a program or script, Python compiles it on-the-fly into what is called "byte code." This code is platform-independent and is simply a low-level representation of your script that is then executed by your computer.

Instructions

    • 1

      Create your Python script using a text editor or a development environment such as IDLE. To use a text editor, open a new file using the text editor -- not word processor -- of your choice. Emacs, vim and Text Wrangler are popular choices.

      To use IDLE, start IDLE. On the Mac, open "Applications," followed by "Python 2.x" then "IDLE." In Windows, click the "Start" menu, followed by "Programs," then "Python 2.x" followed by "IDLE (Python GUI)."

    • 2

      Save the file as "pythonexample.py." This is particularly important if you're using IDLE, since IDLE does not save the file as a ".py" file by default. In many text editors, saving the file as a ".py" file enables many context-dependent syntax hints such as colored text and bracket completions.

    • 3

      Run the completed ".py" file to compile and run its enclosed Python program. To run the script, open a command prompt in Windows or a terminal in a Mac. At the prompt, type "python pythonexample.py" without quotes. Python compiles the enclosed script and runs the contents.

Tips & Warnings

  • When writing Python scripts, you must use text editor such as Notepad, emacs or vim, not a word processor such as Microsoft Word. Word processors add end-of-line and other invisible characters that are illegible to the Python compiler.

Related Searches:

References

Comments

Related Ads

Featured