How to compile python script and create .pyc file?

By: Python Documentation Team Emailed: 1644 times Printed: 2110 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

When a module is imported for the first time (or when the source is more recent than the current compiled file) a .pyc file containing the compiled code should be created in the same directory as the .py file.

One reason that a .pyc file may not be created is permissions problems with the directory. This can happen, for example, if you develop as one user but run as another, such as if you are testing with a web server. Creation of a .pyc file is automatic if you’re importing a module and Python has the ability (permissions, free space, etc...) to write the compiled module back to the directory.

Running Python on a top level script is not considered an import and no .pyc will be created. For example, if you have a top-level module abc.py that imports another module xyz.py, when you run abc, xyz.pyc will be created since xyz is imported, but no abc.pyc file will be created since abc.py isn’t being imported.

If you need to create abc.pyc – that is, to create a .pyc file for a module that is not imported – you can, using the py_compile and compileall modules.

The py_compile module can manually compile any module. One way is to use the compile() function in that module interactively:

>>> import py_compile
>>> py_compile.compile('abc.py')


This will write the .pyc to the same location as abc.py (or you can override that with the optional parameter cfile).

You can also automatically compile all files in a directory or directories using the compileall module. You can do it from the shell prompt by running compileall.py and providing the path of a directory containing Python files to compile:

python -m compileall .


Python Home | All Python Tutorials | Latest Python Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Python Documentation Team
with in Python
goto in Python
Garbage Collection in Python
switch case statement in Python
Overloading in python
Static in python
Delegation in python
Schwartzian Transform in python
Multidimensional list (array) in python
Array in python
Remove duplicates from a list in python
Iterate over a sequence in reverse order in python
Convert between tuples and lists in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
Convert number to string in python

More Tutorials in Python
What is the need for Python language?
How to compile python script and create .pyc file?
How to find the current module name in python
How to force rereading of a changed module in python
UnboundLocalError in python
call by reference in python
Callable objects in python
Ternary operator in python
Hexadecimal and Octal integers in python
Convert string to number in python
Convert number to string in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
Convert between tuples and lists in python
Iterate over a sequence in reverse order in python
Remove duplicates from a list in python

More Latest News
Most Viewed Articles (in Python )
switch case statement in Python
Delegation in python
Iterate over a sequence in reverse order in python
Remove duplicates from a list in python
Installing gedit for python programming in Windows
Formatted printing in Python
How to compile python script and create .pyc file?
Ternary operator in python
Convert between tuples and lists in python
Installing gedit for python programming in Linux
Your First Program in Python
Comments And Pound Characters in Python
Strings And Text in Python
error: â_mysql_ConnectionObjectâ has no member named
What is the need for Python language?
Most Emailed Articles (in Python)
Perl's chomp() equivalent for removing trailing newlines from strings in python
switch case statement in Python
Using Unicode Literals in Python
How to find the current module name in python
Variables And Printing in Python
Strings And Text in Python
error: â_mysql_ConnectionObjectâ has no member named
How to force rereading of a changed module in python
Ternary operator in python
Convert string to number in python
Multidimensional list (array) in python
Installing gedit for python programming in Linux
Your First Program in Python
UnboundLocalError in python
Callable objects in python