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
时间: 2024-10-10 08:49:49

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

File upload error - unable to create a temporary file

php上传图片的时候会报错: File upload error - unable to create a temporary file 文件上传错误 - 无法创建一个临时文件 你只需要打开你的php.ini文件,设置upload_tmp_dir="f:\temp"  然后重启环境就可以了,如果还不行就去看看文件夹的权限是否有权限,赋予写入权限就好了

mysql报Can't create/write to file '/tmp/ib0n3frL' (Errcode: 13 - Permission denied)

初始化数据库报错 [[email protected] mysql-5.6.24]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data Installing MySQL system tables...2017-04-03 19:52:21 0 [Warning] TIMESTAMP with implicit DEFA

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

mysql操作时,出现报错. 执行describe 命令时, 临时文件目录没有创建或者无写入权限:于是: cd /var/lib/mysql/ #进入mysql数据目录 mkdir tmp #创建需要的临时目录 chown mysql.mysql /var/lib/mysql/tmp #给临时目录分配mysql权限用户及群组 ERROR 1 (HY000): Can't create/write to file '/tmp/#sql_830_0.MYI' (Errcode: 13)

编译安装mysql ERROR: 1 Can't create/write to file '/tmp/#sql_86b_0.MYI'

之前安装一个5.1版本,试了一个5.5版本,useradd -s /sbin/nologin -M mysql   创建一个mysql用户mkdir -p /data/mysql chmod -R /data/mysql cd /usr/local/mysqlscripts/mysql_install_db --user=mysql --datadir=/data/mysql出现报错:ERROR: 1  Can't create/write to file '/tmp/#sql_86b_0.MY

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: 13) 两种解决办法:方法一:1.chmod 777 /tmp[[email protected] /]# chmod 777 /tmpchmod: 更改"/tmp" 的权限: 只读文件系统 --遇到这种情况,tmp权限无法更改. 2./etc/init.d/mysqld start

How do I create a zip file?(转)

Creating a zip file is a task that can easily be accomplished by using the classes ZipOutputStream and ZipEntry in the java.util.zip package. First of all, I'm going to present to you a complete code snippet that will create a zip file containing all

ERROR: ORA-09925: Unable to create audit trail file Linux-x86_64

今天手工建库的过程中遇到了这么个错误 ERROR: ORA-09925: Unable to create audit trail file Linux-x86_64 Error: 2: No such file or directory Additional information: 9925 ORA-01075: you are currently logged on [[email protected] dbhome_1]$ ps -ef | grep smon oracle   1190

ORA-09925: Unable to create audit trail file 在DBCA时

ORA-09925: Unable to create audit trail file 在DBCA时 ***数据库后,再执行dbca时候报错 ORA-09925: Unable to create audit trail file  起初以为是权限问题, $cd $ORACLE_HOME/rdbms/audit 查看下权限,属主,为了避免麻烦直接赋予aufit目录 777权限 退出DBCA直接们再次执行,问题依旧 ORA-09925: Unable to create audit trail 

PHP Warning: File upload error - unable to create a temporary file in Unknown on line 0

代码在本地运行一切都OK,放到服务器上,网站访问正常,上传就出现该错误. 提示:PHP Warning: File upload error - unable to create a temporary file in Unknown on line 0 出现问题的服务器:Windows server 2012 Standard iis8 php5.5.37 fastCGI 原因:原来是在配置php环境的时候,php.ini没有设置upload_tmp_dir . 如果 php.ini 没有设置