Python compilation

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

+43
compiler-construction python
Sep 16 '09 at 16:42
source share
8 answers

python yourfile.py

You must install python first. It will automatically compile your file into a binary .pyc file, and then run it for you. It will be automatically recompiled at any time when your file changes.

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

+27
Sep 16 '09 at 16:44
source share

Python compiles its files into bytecode before executing them. This means that the Python interpreter must be installed on the target machine.

If you do not want to install Python on the target machine, use py2exe , py2app, or something similar.

+15
Sep 16 '09 at 16:48
source share

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

 compileall.py <directory> 

this command will compile python code in this directory recursively

compileall script is usually located in a directory e.g.

 /usr/local/lib/python2.6 

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

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

compileall can also be used as a module

 import compileall compileall.compile_dir(path) 
+10
Oct 07 '09 at 1:27
source share

Python is an interpreted language, so you do not need to compile it; just to run it. As it happens, the standard version of python will compile it into "bytecode", like Java, etc., And save it (in .pyc files) and run it next time, saving time if you did not update the file since. If you updated the file, it will be automatically recompiled.

You can also run python with the -O flag, which will generate .pyo files instead of .pyc. I am not sure that this is of great importance. If speed is important, use psyco.

And yes, on Unix (including Linux, BSD, and Mac OS X, or in the unix shell on windows) you can use the shebang line at the top of the file so that the file starts automatically using python. On Windows, the equivalent is to link the .py files to the python.exe file, and then make sure the PATHEXT environment variable includes the .PY extension.

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

+8
Sep 16 '09 at 17:44
source share

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” must be associated with the python executable, allowing it to be run as follows:

 > myfile.py 

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

 $ ./myfile.py 

On * nix systems, if the first two characters of the file are #! then it will execute the file with the specified executable, which I installed here as /usr/bin/python .

+5
Sep 16 '09 at 16:54
source share

If you want to convert the python source file to double-clicking .exe on windows, you can use py2exe , which can help you create an easy-to-distribute package.

+3
Sep 16 '09 at 17:09
source share

On most Unix-like systems, you can use shebang to tell the operating system that the interpreter should invoke. You just bet

 #!/path/to/python 

in the first line of your file, where, of course, you should replace "/ path / to /" with the path that you have on your system. In most cases, it will be "/ usr / bin / python" or "/ usr / local / bin / python". On Unix systems, you can also search for a path using

 "#!usr/bin/env python" 

or call the command

 which python 

to find the way. Then you can run your program with the command

 ./yourprogram.py 

If he tells you that you do not have permission to do this, you should use the command

 chmod a+x yourprogram.py 
+1
Sep 16 '09 at 17:01
source share

Answer for Windows

  • you must install python first
  • then set the path variable
  • after that write your python program and save
  • think there is a python program whose name is < hello.py "
  • open cmd.exe
  • then follow the path where you saved the hello.py file,
  • and then type python hello.py and press enter .

now python code is automatically compiled and shows the result.

0
Nov 13 '13 at 7:26
source share



All Articles