Python automatically compiles your script to compiled code, so called byte code, before running it.

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 will usually be created in the same directory as the .py file. When you run the program next time, Python uses this file to skip the compilation step.

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 a script is not considered an import and no .pyc will be created. For example, if you have a script file 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 a .pyc file for a module that is not imported, you can use the py_compile and compileall modules.

The py_compile module can manually compile any module. One way is to use the py_compile.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 (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.

python -m compileall .

If the directory name (the current directory in this example) is omitted, the module compiles everything found on sys.path.

If you’re curious, you can look at the byte code using the dis module:

>>> def hello():
... print "hello!"
...
>>> dis.dis(hello)
2 0 LOAD_CONST 1 ('hello!')
3 PRINT_ITEM
4 PRINT_NEWLINE
5 LOAD_CONST 0 (None)
8 RETURN_VALUE

How do I create a .pyc file?的更多相关文章

  1. 解决: org.iq80.leveldb.DBException: IO error: C:\data\trie\000945.sst: Could not create random access file.

    以太坊MPT树的持久化层是采用了leveldb数据库,然而在抽取MPT树代码运行过程中,进行get和write操作时却发生了错误: Caused by: org.fusesource.leveldbj ...

  2. sqlplus链接数据库报ORA-09925: Unable to create audit trail file

    [localhost.localdomain]:[/oracle11/app/oracle11/product/11.2.0/dbhome_1/dbs]$ sqlplus / as sysdba SQ ...

  3. Can't create/write to file '/tmp/#sql_887d_0.MYD' (Errcode: 17)

    lsof |grep "#sql_887d_0.MYD" 如果没有被占用就可以删掉 . https://wordpress.org/support/topic/cant-creat ...

  4. ERROR 1 (HY000): Can't create/write to file '/tmp/#sql_909_0.MYI' (Errcode: 13)

    mysql> desc tablename; ERROR 1 (HY000): Can't create/write to file '/tmp/#sql_909_0.MYI' (Errcode ...

  5. Can't create/write to file '/tmp/#sql_3105_0.MYI' (Errcode: 13)

    最近的项目中由于临时存储空间太大了.索性把tmp目录删除了.结果访问出现 Can't create/write to file '/tmp/#sql_3105_0.MYI' (Errcode: 13) ...

  6. [问题解决] initAndListen: 10309 Unable to create/open lock file: /data/db/mongod.lock

    错误: 在linux下开启mongoDB的 $ >bin: ./mongod 时报错:initAndListen: 10309 Unable to create/open lock file: ...

  7. How do I create a zip file?(转)

    Creating a zip file is a task that can easily be accomplished by using the classes ZipOutputStream a ...

  8. OUI-67076 : OracleHomeInventory was not able to create a lock file" in Unix

    Symptoms The command "opatch lsinventory" reports the error: OUI-67076:OracleHomeInventory ...

  9. How to create a zip file in NetSuite SuiteScript 2.0 如何在现有SuiteScript中创建和下载ZIP压缩文档

    Background We all knows that: NetSuite filecabinet provided a feature to download a folder to a zip ...

随机推荐

  1. 学习FPGA需要做哪些

    有些人比较差,做了一些介绍,有误导成分.有些人水平太高,介绍的很好,但是很多人依旧听不懂,得到的肯定很少.学习FPGA,在不同层次的人明显有不同的答案. 熟悉硬件描述语言语法,不需要什么都会,但是要记 ...

  2. PCIE接口的说明

    https://blog.csdn.net/u013253075/article/details/80835489

  3. PHP交易详情有感

    交易详情 一般都是按月的, 包含,交易日期,交易金额,交易状态(可有可无) 总交易额等等. 如果数据多的话,最好能够分页. 最好能够查询具体的哪一个商户. 1.模拟sql实现查询功能 SELECT a ...

  4. Oracle 11g r2 rac +openfiler 2.99 安装

    1 openfiler 2.99 安装 在官网下载iso文件,这里选择openfileresa-2.99.1-x86_64-disc1.iso 版本,在vbox下创建一个虚拟机 --vbox 选择li ...

  5. explain解析

    MySQL执行计划调用方式执行计划包含的信息执行计划显示内容解读MySQL执行计划的局限MySQL5.6支持OPTIMIZER_TRACE 1.什么是归并排序?将已有序的子序列合并,得到完全有序的序列 ...

  6. mysql字符类型

    字符类型 #官网:https://dev.mysql.com/doc/refman/5.7/en/char.html #注意:char和varchar括号内的参数指的都是字符的长度 #char类型:定 ...

  7. mysql表的增删改查

    一.表介绍 表相当于文件,表中的一条记录就相当于文件的一行内容,不同的是,表中的一条记录有对应的标题,称为表的字段 id,name,qq,age称为字段,其余的,一行内容称为一条记录 二.创建表 cr ...

  8. oracle的sqlldr时插入新列和固定数据

    ctl文件加入固定值 region CONSTANT '31', 加入默认时间 RECORD_DATE "sysdate" 最好数据也设置RECORD_DATE的默认值为sysda ...

  9. Linux性能监测:内存篇

    在操作系统里,虚拟内存被分成页,在 x86 系统上每个页大小是 4KB.Linux 内核读写虚拟内存是以 “页” 为单位操作的,把内存转移到硬盘交换空间(SWAP)和从交换空间读取到内存的时候都是按页 ...

  10. java成神之——线程操作

    线程 Future CountDownLatch Multithreading synchronized Thread Producer-Consumer 获取线程状态 线程池 ThreadLocal ...