To speed up loading modules, Python caches the compiled version of each module in the __pycache__ directory under the name module.version.pyc, where the version encodes the format of the compiled file; it generally contains the Python version number. For example, in CPython release 3.3 the compiled version of spam.py would be cached as __pycache__/spam.cpython-33.pyc. This naming convention allows compiled modules from different releases and different versions of Python to coexist.

Python checks the modification date of the source against the compiled version to see if it’s out of date and needs to be recompiled. This is a completely automatic process. Also, the compiled modules are platform-independent, so the same library can be shared among systems with different architectures.

Python does not check the cache in two circumstances. First, it always recompiles and does not store the result for the module that’s loaded directly from the command line. Second, it does not check the cache if there is no source module. To support a non-source (compiled only) distribution, the compiled module must be in the source directory, and there must not be a source module.

Some tips for experts:

  • You can use the -O or -OO switches on the Python command to reduce the size of a compiled module. The -O switch removes assert statements, the -OO switch removes both assert statements and __doc__ strings. Since some programs may rely on having these available, you should only use this option if you know what you’re doing. “Optimized” modules have a .pyo rather than a .pyc suffix and are usually smaller. Future releases may change the effects of optimization.
  • A program doesn’t run any faster when it is read from a .pyc or .pyo file than when it is read from a .py file; the only thing that’s faster about .pyc or .pyo files is the speed with which they are loaded.
  • The module compileall can create .pyc files (or .pyo files when -O is used) for all modules in a directory.
  • There is more detail on this process, including a flow chart of the decisions, in PEP 3147.

“Compiled” Python files的更多相关文章

  1. compiled python files

    [compiled python files] As an important speed-up of the start-up time for short programs that use a ...

  2. [TypeScript] Loading Compiled TypeScript Files in Browser with SystemJS

    TypeScript outputs JavaScript, but what are you supposed to do with it? This lesson shows how to tak ...

  3. py, pyc, pyw, pyo, pyd 及发布程序时的选择 Compiled Python File (.pyc)

    Python 程序扩展名(py, pyc, pyw, pyo, pyd)及发布程序时的选择 - 司开星的专栏 - CSDN博客 https://blog.csdn.net/chroming/artic ...

  4. py, pyc, pyw, pyo, pyd Compiled Python File (.pyc) 和Java或.NET相比,Python的Virtual Machine距离真实机器的距离更远

    https://my.oschina.net/renwofei423/blog/17404 1.      PyCodeObject与Pyc文件 通常认为,Python是一种解释性的语言,但是这种说法 ...

  5. Python模块学习

    6. Modules If you quit from the Python interpreter and enter it again, the definitions you have made ...

  6. 记录一下跟Python有关的几个拓展名

    .py python文本源码文件,也可以用python.exe直接运行 .pyw 也是python的文本源码文件,但是默认由pythonw.exe打开,而且不显示命令行窗口,带GUI的python代码 ...

  7. Python Tutorial 学习(六)--Modules

    6. Modules 当你退出Python的shell模式然后又重新进入的时候,之前定义的变量,函数等都会没有了. 因此, 推荐的做法是将这些东西写入文件,并在适当的时候调用获取他们. 这就是为人所知 ...

  8. [译]The Python Tutorial#6. Modules

    [译]The Python Tutorial#Modules 6. Modules 如果你从Python解释器中退出然后重新进入,之前定义的名字(函数和变量)都丢失了.因此,如果你想写长一点的程序,使 ...

  9. Python Module模块

    模块 https://docs.python.org/zh-cn/3/tutorial/modules.html 模块的概念被高级语言广泛使用. Python的定义 一个包括Python定义和语句的文 ...

随机推荐

  1. 【如何快速的开发一个完整的iOS直播app】(推流篇)

    前言 在看这篇之前,如果您还不了解直播原理,请查看这篇文章如何快速的开发一个完整的iOS直播app(原理篇) 开发一款直播app,肯定需要流媒体服务器,本篇主要讲解直播中流媒体服务器搭建,并且讲解了如 ...

  2. SVMshow

    SVMshow % http://www.peteryu.ca/tutorials/matlab/visualize_decision_boundaries % load RankData % Num ...

  3. 5月25日 DOM

    练习一:输入答案,弹出是否正确. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...

  4. SASS学习笔记1 —— 安装、编译和调试

    一.什么是SASS SASS是一种"CSS预处理器"(css preprocessor)的开发工具,为CSS加入编程元素,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的 ...

  5. go——搭建Win7下的Go开发环境

    1.首先需要下载下载go平台安装包 安装程序 下载地址:https://golang.org/dl/ (墙内下载地址http://www.golangtc.com/download),如果是您的系统是 ...

  6. 230. Kth Smallest Element in a BST ——迭代本质:a=xx1 while some_condition: a=xx2

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  7. GO工程和包

    转载:http://blog.csdn.net/achelloworld/article/details/43956831 在Go语言中,包是函数与数据的集合,用package定义一个包,包的名字不一 ...

  8. JDE开发端安装笔记

    JDE版本:JD Edwards EnterpriseOne 9.1.4 for Microsoft 64bit Oracle客户端:Oracle Database 11g Release 2 Cli ...

  9. jQuery给同一个元素两个点击事件

    $(".course-form .course-start img").each(function(i) { $(this).toggle(function(){ $(this). ...

  10. 快速将excel数据保存到Oracle数据库中【转】

    我们在工作中,也许会碰到以下情况,客户或者同事发来需要调查的数据,并不是dmp文件,而是excel文件,此时通常是一张表,少量几条记录.最近我恰好碰到了这种情况,所以做了些调查,不敢藏私,拿出来跟大家 ...