I have a Python script around 5,000 lines long. If I make a single change in the file, do I have to recompile the entirety of the file, or is it possible to compile only that part or particular code?
Asked
Active
Viewed 265 times
0
-
5Is this a trick question? I thought Python was interpreted, not compiled. – Kevin Mar 30 '15 at 19:15
-
1Must be a trick question. – miradulo Mar 30 '15 at 19:16
-
1You can compile python code and save it as pyc ... http://stackoverflow.com/questions/471191/why-compile-python-code – Bhargav Rao Mar 30 '15 at 19:17
-
Also, why do you have a single script 5,000 lines long?! – jonrsharpe Mar 30 '15 at 19:17
-
during import python interpretor compile our code to byte code i am talking about that file. – Aman Jain Mar 30 '15 at 19:17
-
@josrsharpe, actually its my main script. – Aman Jain Mar 30 '15 at 19:18
-
1@Kevin Is this a trick comment? I thought Python was [compiled into a bytecode format before execution](https://docs.python.org/3/glossary.html#term-bytecode). – Carsten Mar 30 '15 at 19:19
-
A .pyc file is a binary file containing these: A four-byte magic number, a four-byte modification timestamp, and a marshalled code object. If you can figure out a way to modify the code object and timestamp, then your job is done. http://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html – Jerry Ajay Mar 30 '15 at 19:25
-
i mean compile into bytecode, guys dont do that please ans if you want to. – Aman Jain Mar 30 '15 at 19:28
-
1Ok, I admit that _C_Python has a compilation step ;-) In any case, consider refactoring your large source file into many smaller files. It's good style, and I expect it will cut down on compilation time. – Kevin Mar 30 '15 at 19:31
-
actually my main scenerio is, i am using interpretor to import a module after import i made some changes to my main file, how to recompile only that change code, i dont want to use reload method bcs it will recopile entire file. is it possible or not??? – Aman Jain Mar 30 '15 at 19:39
-
@AmanJain -- compiling and importing are 2 different things. What you want to do is to re _import_ a portion of the file. That's not possible. – mgilson Mar 30 '15 at 19:41
1 Answers
0
CPython will re-generate the bytecode for an altered source file from scratch. There is no 'partial' compilation option for just the lines that you changed. Recompilation is transparent and automatic.
I'm not aware of any Python implementation that would behave otherwise.
I'd not be worried about speed here though; compiling a 5000 line module doesn't take all that long. But from a maintenance point of view you should still look into refactoring your module into a smaller separate modules.

Martijn Pieters
- 1,048,767
- 296
- 4,058
- 3,343