• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

How to Compile Code to Convert py to pyc in Python?

Aniruddha Chaudhari/162099/9
CodePython

Do you want to convert py to pyc in Python?

Let’s get some background understanding…

Table of Contents

  • What is pyc file in Python?
  • When does .pyc file get created?
  • Convert py to pyc in Python  by Compiling without Executing
    • Method 1: using py_compile module
    • Method 2: using compile all module

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 don’t see the .pyc file.

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

py vs pyc file in Python:

If you open any py file, you can see the Python code which is human readable. Whereas, pyc file contains byte code. You can not read it simply.

Now, our interest is to convert the py file into pyc file. For this, you have to compile Python py file without executing it.

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 don’t have write permissions for the directory where you are compiling the code. It can also happen when you don’t 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 let’s 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
py_compile.compile('abc.py')

This will compile the abc.py file and saved compiled bytecode in abc.pyc file. Python stores bytecode in the same directory where the abc.py 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 this post, I have not covered anything related to compiler and interpreter as it is not the scope of this article. If you are interested, you can read the difference between Compiler and Interpreter.

This is all about how to convert py to pyc in Python. If you have any doubt, write in the comment.

Happy Pythoning!

Python Interview Questions eBook

Python
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Jeffrey Greenberg
    January 24, 2020 at 8:15 pm

    Hi Aniruddha,
    Thank you for posting this.
    I needed to know how to compile a library.
    I used method 1.
    could NOT have been easier!

    • Reply
      Aniruddha Chaudhari
      January 24, 2020 at 8:51 pm

      Hi Jeffery,

      Are you getting any error while compiling a library?

  • Reply
    Swapnali
    February 16, 2020 at 10:22 am

    Hi Aniruddha,
    This post is very helpful and exactly what I was looking for. Can you please help me with below questions?
    1. How to execute .pyc file?
    2. Is it possible for anyone to convert .pyc to .py and read/modify the source code?

    • Reply
      Aniruddha Chaudhari
      February 16, 2020 at 12:54 pm

      Hi Swapnil,

      Thanks for your kind words.

      1. You can execute .pyc file through Python interpreter. Simple command: python example.pyc

      2. There are some techniques for doing this. But it is complex and does not give 100% accuracy. Not reliable.

    • Reply
      Sreejith V R
      March 20, 2020 at 11:54 am

      Hi Swapnali,
      Yes, it is possible.
      Use Python decompiler for convert .pyc to .py.
      One of the best python decompiler: “Easy Python Decompiler”.
      You can download it from here.

  • Reply
    Prithvi
    October 6, 2021 at 8:43 am

    I am not able to extract the pyc code from the python code in pycharm.

    • Reply
      Aniruddha Chaudhari
      October 11, 2021 at 2:49 pm

      There is nothing to do with pycharm or any IDE. What procedure you are following and error you are getting?

  • Reply
    Damini Chopra
    November 2, 2021 at 10:26 am

    Hi Aniruddha,

    I have to apply bytecode to my existing python project files. So I have been investigating on Python pyarmor package and converting .py file to .pyc format.

    My question is if i have exisitng whole pyhton project how can I make them not available to end user readable format using these methods or how i can apply to exisitng python project files.

    Thank you in advance!!

    • Reply
      Aniruddha Chaudhari
      January 29, 2022 at 11:29 am

      Hi Damini,

      I’m not sure, if there is way to convert entire project files to bytecode format. This is still scope for me to explore.

      Rather, I would suggest, you can write a script that read individual files in your project and convert them to .pyc file.

Leave a Reply Cancel reply

Basic Python Tutorial

  1. Python- Tutorial Overview
  2. Python- Applications
  3. Python- Setup on Linux
  4. Python- Setup on Windows
  5. Python- Basic Syntax
  6. Python- Variable Declaration
  7. Python- Numeric Data Types
  8. Python- NoneType
  9. Python- if-else/elif
  10. Python- for/while else
  11. Python- User Input
  12. Python- Multiline User Input
  13. Python- String Formatting
  14. Python- Find Substring in String
  15. Python- Bitwise Operators
  16. Python- Range Function
  17. Python- List
  18. Python- List Vs Tuple
  19. Python- Compare Two Lists
  20. Python- Sorting List
  21. Python- Delete Element from List
  22. Python- Dictionary
  23. Python- ‘is’ vs ‘==’
  24. Python- Mutable vs Immutable
  25. Python- Generator & Yield
  26. Python- Fibonacci Generator
  27. Python- Assert Statement
  28. Python- Exception Handling 
  29. Python- RegEx
  30. Python- Lambda Function
  31. Python- Installing Modules
  32. Python- Important Modules
  33. Python- Find all Installed Modules
  34. PyCharm- IDE setup
  35. Python- File Handling
  36. Python- Monkey Patching
  37. Python- Decorators
  38. Python- Instance vs Static vs Class Method
  39. Python- Name Mangling
  40. Python- Working with GUI
  41. Python- Read Data from Web URL
  42. Python- Memory Management
  43. Python- Virtual Environment
  44. Python- Calling C Function

Python Exercise

  1. Python- Tricky Questions
  2. Python- Interview Questions (60+)
  3. Python- Project Ideas (45+)
  4. Python- MCQ Test Online
  5. Python- Coding Questions (50+)
  6. Python- Competitive Coding Questions (20+)

Python String

  1. Reverse the String
  2. Permutations of String
  3. Padding Zeros to String/Number

Python List

  1. Randomly Select Item from List
  2. Find Unique Elements from List
  3. Are all Elements in List Same?

Python Dictionary

  1. Set Default Value in Dictionary
  2. Remove all 0 from a dictionary

File Handling

  1. Python- Read CSV File into List
  2. Check if the File Exist in Python
  3. Find Longest Line from File

Compilation & Byte Code

  1. Multiple Py Versions on System
  2. Convert .py file .pyc file
  3. Disassemble Python Bytecode

Algorithms

  1. Sorting- Selection Sort
  2. Sorting- Quick Sort

Other Python Articles

  1. Clear Py Interpreter Console
  2. Can I build Mobile App in Python?
  3. Extract all the Emails from File
  4. Python Shell Scripting

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard