![]() |
|
Compiling Your Python Project - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Python (https://sinister.li/Forum-Python) +--- Thread: Compiling Your Python Project (/Thread-Compiling-Your-Python-Project) |
Compiling Your Python Project - Ex094 - 02-19-2013 Hello [username]! Python Version used is 3.3 I'll be showing you how to compile your python project using python's built-in module called "py_compile" 1) So first of all open your Python IDE and then type: Code: import py_compileNow you should know the proper path to where your .py (project file) is present. 2) Next type in your Python IDE: Code: py_compile.compile('Path_to_your_project.py')3) Go to your project folder and you'll have your compiled Python File in the "py_cache" folder. RE: Compiling Your Python Project - zero-uplink - 06-23-2013 Why do we want to compile it ? And by compiling, which format do the .py turns into? RE: Compiling Your Python Project - Ex094 - 06-23-2013 (06-23-2013, 04:49 AM)zero-uplink Wrote: Why do we want to compile it ? Why Compile It? Well that is a silly question to ask In my opinion because as you work is present in a compiled for you'll be able to execute it directly instead of running it from the python command in command line. After compiling, the format becomes .pyc RE: Compiling Your Python Project - Psycho_Coder - 06-23-2013 Hey Ex094 I think this short thread should be Prefixed with tutorial tag. RE: Compiling Your Python Project - Deque - 06-23-2013 (06-23-2013, 04:56 AM)Ex094 Wrote:(06-23-2013, 04:49 AM)zero-uplink Wrote: Why do we want to compile it ? I don't think that's a silly question. After all you are making tutorials for beginners. Someone who doesn't know how to compile most certainly doesn't know why to compile. So answering that question in your tutorial would be an improvement. RE: Compiling Your Python Project - Ex094 - 06-23-2013 (06-23-2013, 08:05 AM)Deque Wrote:(06-23-2013, 04:56 AM)Ex094 Wrote:(06-23-2013, 04:49 AM)zero-uplink Wrote: Why do we want to compile it ? Agreed! He asked that question in such a way which made me thing he knows about it RE: Compiling Your Python Project - zero-uplink - 06-23-2013 (06-23-2013, 08:08 AM)Ex094 Wrote:(06-23-2013, 08:05 AM)Deque Wrote:(06-23-2013, 04:56 AM)Ex094 Wrote:(06-23-2013, 04:49 AM)zero-uplink Wrote: Why do we want to compile it ? My Apologies My English was not really good. I just don't know why to compile. The only thing I know about the term "Compile" in python is, making exe out of .py I have another question. When we import a module/library for the first time, python do compile it into .pyc right? But why not the script that imported the module or library? It seems to me that python only compile something that is being imported. Not the module or script which imported it. May be a silly question too. But I really don't know. RE: Compiling Your Python Project - Ex094 - 06-23-2013 (06-23-2013, 10:00 AM)zero-uplink Wrote:(06-23-2013, 08:08 AM)Ex094 Wrote:(06-23-2013, 08:05 AM)Deque Wrote:(06-23-2013, 04:56 AM)Ex094 Wrote:(06-23-2013, 04:49 AM)zero-uplink Wrote: Why do we want to compile it ? I should be the one to apologize! Well in python the .pyc is called Python Compiled Code so no need to mix it up with .exe There's another module called "compileall" which compiles all the module in a directory. The syntax is similar to pycompile just the name is changed like: Code: import compileall
compileall.compile_dir("mylib", force=1)May be this will help http://effbot.org/zone/python-compile.htm RE: Compiling Your Python Project - Deque - 06-23-2013 (06-23-2013, 10:00 AM)zero-uplink Wrote: I have another question. Let me explain this a bit. A programming language can have a lot of different implementations, in Python these are i.e. IronPython, CPython, Jython, ... Depending on the implementation the process of compiling and interpreting is handled differently. The .pyc files are a result of the CPython implementation. Everytime you type in "python myfile.py" into your console the script is compiled into .pyc, which is a bytecode. Bytecode is an intermediate language that is afterwards interpreted by a virtual machine. So everytime you run the script, it is compiled again. That costs time (at startup). If you know you want to run your script very often without the need of recompiling it, you can compile it by yourself and run the .pyc files instead. So you save the time of compilation when running the script. That's the only difference. But every .py file gets compiled in one way or the other. RE: Compiling Your Python Project - mls577 - 06-26-2013 and for the lazy mofo's like myself, I present my I'm too lazy to type all this shit out everytime I need to compile file: with sys.argv Code: import sys
import py_compile
if(len(sys.argv) == 2):
print("usage: ./compile.py <location and file name>")
file = sys.argv[1]
py_compile.compile(file)
else:
print("usage: ./compile.py <location and file name>")using input() instead Code: import py_compile
file = input("Enter the python location and file name: ")
py_compile.compile(file) |