
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Can we keep Python modules in compiled format?
Yes you can keep Python modules in compiled format. Python automatically compiles Python source code when you import a module, so the easiest way to create a PYC file is to import it. If you have a module mymodule.py, just do:
>>> import mymodule
to create a mymodule.pyc file in the same directory. A drawback is that it doesn’t only compile the module, it also executes it, which may not be what you want. (however, it does compile the entire script even if it fails to execute the script). To do this programmatically, and without executing the code, you can use the 'py_compile' module:
>>> import py_compile >>> py_compile.compile("mymodule.py")
There’s also a 'compileall' module which can be used to compile all modules in an entire directory tree.
>>> import compileall >>> compileall.compile_dir("mylib", force=1)
Note that Python’s byte code is portable between platforms, but not necessarily between Python releases.
- Related Articles
- How we can import Python modules in Jython?
- How we can bundle multiple python modules?
- How we can import Python modules without installing?
- Can we iteratively import python modules inside a for loop?
- How we can copy Python modules from one system to another?
- Program to find index, where we can insert element to keep list sorted in Python
- How can we display all modules with classloaders in Java 9?
- How do we use easy_install to install Python modules?
- Reloading modules in Python?
- Locating Modules in Python
- In MySQL, how can we use FROM_UNIXTIME() function with format string?
- How can I import modules for a Python Azure Function?
- XML Processing Modules in Python
- How can I represent python tuple in JSON format?
- How can we format a date using the Jackson library in Java?
