How do I create a .pyc file?
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?的更多相关文章
- 解决: 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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) ...
- [问题解决] initAndListen: 10309 Unable to create/open lock file: /data/db/mongod.lock
错误: 在linux下开启mongoDB的 $ >bin: ./mongod 时报错:initAndListen: 10309 Unable to create/open lock file: ...
- 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 ...
- OUI-67076 : OracleHomeInventory was not able to create a lock file" in Unix
Symptoms The command "opatch lsinventory" reports the error: OUI-67076:OracleHomeInventory ...
- 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 ...
随机推荐
- videojs集成--播放rtmp流
之前说到已经把流推送过来了,这时候就可以使用videojs来进行显示播放. 首先要先有一个文件,那就是video-js.swf 因为,这种播放方式html已经不能很好的进行播放了,需要用到flash来 ...
- 2017中国大学生程序设计竞赛 - 女生专场(Graph Theory)
Graph Theory Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)To ...
- Dynamics CRM 2011 通过工作流发邮件时的权限问题
场景: 在CRM中配置工作流,完成某个步骤后,发送邮件通知其他用户.发件人统一配置为管理员,收件人则根据业务需要设定动态值. 相关权限配置 首先启动流程的用户, 需要允许其他用户代表发送电子邮件 另外 ...
- springboot成神之——log4j2的使用
本文介绍如何在spring-boot中使用log4j2 说明 依赖 日志记录语句 log4j2配置文件 本文介绍如何在spring-boot中使用log4j2 说明 log4j2本身使用是非常简单的, ...
- pandas层级索引1
层级索引(hierarchical indexing) 下面创建一个Series, 在输入索引Index时,输入了由两个子list组成的list,第一个子list是外层索引,第二个list是内层索引. ...
- Eclipse修改XML默认打开方式
用Eclipse开发Android的时候 默认的XML是采用Android xml editor 打开,这个工具不够直观,如果想直接看文本的XML的话,可以通过如下方式修改 1.菜单:Window ...
- Asp.NetCore远程自启动、重启、关闭实现
一.背景 NetCore作为微服务可以注册到服务中心,服务中心可以远程启动.重启.关闭该微服务 二.实现 1.创建一个NetCore 2.0 WebApi项目 2.创建一个进程去管理NetCore程序 ...
- Lyx/LaTeX笔记04---插入伪代码
1 可用的宏包 常用的排版伪代码包有clrscode, algorithm, algorithmic, algorithmicx, algorithm2e 2 clrscode clrscode是著名 ...
- python文件处理-读、写
Python中文件处理的操作包括读.写.修改,今天我们一起来先学习下读和写操作. 一.文件的读操作 例一: #文件读操作 f = open(file="first_blog.txt" ...
- Collision Detection
[Collision Detection] Collision Detection是Rigidbody中的一个属性.所以显然Collision Detection指定的类型只在Rigidbody之间才 ...