Python : py_compiler |Convert py to pyc

Python : py_compiler |Convert py to pyc

Convert py to pyc

What is pyc file in Python?

Whenever you run the Python code, there are two major steps involved.

  • The code (from .py file) gets compiled into byte code and save it as a .pyc file.
  • Byte code from this file gets interpreted.

Python is called as a Byte code interpreted programming language.

This complete process happens in the background so that you dont see the .pyc file.

This is the usual case when you simply execute any Python program.

Difference between .py and .pyc files?

Python compiles the .py files and saves it as .pyc files , so it can reference them in subsequent invocations. The .pyc contain the compiled bytecode of Python source files. The .pyc contain the compiled bytecode of Python source files, which is what the Python interpreter compiles the source to. This code is then executed by Python's virtual machine . There's no harm in deleting them (.pyc), but they will save compilation time if you're doing lots of processing.

Python is an interpreted language , as opposed to a compiled one, though the distinction can be blurry because of the presence of the bytecode compiler. Compiling usually means converting to machine code which is what runs the fastest. But interpreters take human readable text and execute it. They may do this with an intermediate stage .

For example, When you run source file, the python interpreter first looks to see if any 'myprog.pyc' (which is the byte-code compiled version of '') exists, and if it is more recent than ''. If so, the interpreter runs it. If it does not exist, or '' is more recent than it (meaning you have changed the source file), the interpreter first compiles '' to 'myprog.pyc'.

There is one exception to the above example. If you put '#! /usr/bin/env python' on the first line of '', make it executable , and then run '' by itself.

When does .pyc file get created?

We import modules in our program. If you are importing module first time, Python compiles the module code and create .pyc file. It is nothing but the bytecode file.

In another scenario, if there is any change in code, python compiles the code and save it as bytecode (.pyc file) before running a python script.

The .pyc file is created in the same folder where the .py file exists.

These are all usual ways, Python compiles and generates the .pyc file.

Why does compiling .py to .pyc not happen?

In certain scenarios, Python does not generate the .pyc file.

This may occur in case you dont have write permissions for the directory where you are compiling the code. It can also happen when you dont have enough space to store bytecode (.pyc file).

In this case, if you are running Python code, it may run existing (old) .pyc bytecode. This is the worst behavior to trace.

I still remember when I faced this issue. I was updating my code in a python script, but when I run the code, I was amazed not to see any changes in output even after doing changes in the Python script.

And here is a way to get rid of it.

Convert py to pyc in Python  by Compiling without Executing

So lets see how to create a .pyc file just with a simple command.

For this, you can use two modules for compiling Python script- py_compile and compileall modules.

Method 1: using py_compile module

Import py_compile in your python code. There is a function called compile(). Pass the Python file name that you want to compile as an argument. It will convert Python script into the bytecode (file with the file extension pyc).

import py_compile

This will compile the file and saved compiled bytecode in abc.pyc file. Python stores bytecode in the same directory where the file exists.

Method 2: using compile all module

Sometimes you may need to compile all the python script within a given directory. Then this method is good rather than compiling each file independently.

In that case, run the following command in a given directory.

python -m compileall

It will compile all the files in the current directory.

If you are not providing any of the directories in command, it compiles all the python script found in sys.path.

Note, it will just compile Python py file without executing.

In next article, we will learn to generate byte code in python


More Articles of Diksha Kumari:

Name Views Likes
Python : py_compile | Generate byte code 308 0
Python : py_compiler |Convert py to pyc 1157 0
Python : py_compile |Compile python sources file 287 0
Python :How to lock Critical Sections(Thread based parallelism) 249 0
Python : Locking without Deadlocks(Thread based Parallelism ) 180 0
Python : Communicating Between Threads-2(Thread based parallelism) 389 0
Python : Communicating Between Threads -1(Thread based parallelism) 176 0
Python Start and Stop a thread (Thread based parallelism) 165 0
Python: Check if a Thread has started (thread based parallelism) 144 0
Python Thread-based parallelism :main_thread() 229 0
Python Lock Class | acquire() Method(thread based parallelism) 294 0
Python Thread-based parallelism : get_native_id() 460 0
Python Thread-based parellelism: active_count() 269 1
Python Lock Class | lock() Method(thread based parallelism) 246 0
Python Thread-based parallelism : stack_size([size]) 331 1
Python Thread Local Data(thread based parallelism 265 1
Python Semaphore Objects (thread based parallelism) 264 0
Python Event class - set() method | Thread based parallelism 252 1
Python Condition object (thread based parallelism) 411 0
Python Thread based parallelism : Run() 228 0
Python Thread-based parallelism : current_thread() 253 1
Python Condition object - acquire()(thread based parallelism) 190 0
Python Thread-based parallelism : TIMEOUT_MAX () 423 0
Python Condition object - notify_all() ( thread based parallelism ) 192 0
Python Thread-based parallelism :.settrace(func) 308 0
Python difference lock and RLock object : Thread based parallelism 1082 0
Python Event class - clear() method | Thread based parallelism 188 0
Python Thread object -start() (thread based parallelism) 335 0
Python RLock Class - acquire() Method (thread based parallelism) 226 0
Python Thread-based parallelism : enumerate() 246 0
Python Thread based parallelism: get_ident() 302 1
Python Thread-based parallelism : setprofile(func) 215 0
Python RLock Class - release() Method (thread based parallelism) 194 0
Python Barrier object | Thread based parallelism 228 0
Python Condition object - notify () ( thread based parallelism ) 217 0
Python Event class - is_set() method | Thread based parallelism 173 0
Python Thread based parallelism : join() 317 0
Python thread based parallelism : 200 0
Python Event class - wait() method | Thread based parallelism 243 0
PYTHON Thread based parallism 292 1
Python Barrier method | Thread based parallelism 228 0
Python Condition object - release() ( thread based parallelism ) 184 0
Python Thread based parallelism: excepthook 1487 1
Python thread object : is_alive() (thread based parallelism) 996 0
Python Condition object - wait() ( thread based parallelism ) 215 0
Python Lock Class | release() Method(thread based parallelism) 254 0
Python Thread object (thread based parallelism) 259 0
Python Timer class - start() method | Thread based parallelism 175 1
Python thread based parallelism : 322 1
Python Timer class - cancel() method | Thread based parallelism 206 1
Python thread object : Deamon() (thread based parallelism) 241 0