發佈django pyc版本

第一種簡單方法:

  1. 生產pyc文件: python3 -m compileall -b .
  2. 刪除py文件: find . -name “*.py” |xargs rm -rf
  3. 刪除pycache目錄: find . -name “pycache” |xargs rm -rf

第二種:
項目同名文件下 創建 pyc_compile.py 本地測試linunx環境

import os
import sys
import shutil
from py_compile import compile

#print "argvs:",sys.argv
if len(sys.argv) == 3:
    comd = sys.argv[1]  #輸入的命令
    path = sys.argv[2]  #文件的地址
    if os.path.exists(path) and os.path.isdir(path):
        for parent,dirname,filename in os.walk(path):
            for cfile in filename:
                fullname = os.path.join(parent,cfile)
                if comd == 'clean' and cfile[-4:] == '.pyc':
                    try:
                        os.remove(fullname)
                        print("Success remove file:%s" % fullname)
                    except:
                        print("Can't remove file:%s" % fullname)
                if comd == 'compile' and cfile[-3:] == '.py':   #在這裏將找到的py文件進行編譯成pyc,但是會指定到一個叫做__pycache__的文件夾中
                    try:
                        compile(fullname)
                        print("Success compile file:%s" % fullname)
                    except:
                        print("Can't compile file:%s" % fullname)
                if comd == 'remove' and cfile[-3:] == '.py' and cfile != 'settings.py' and cfile != 'wsgi.py':
                    try:
                        os.remove(fullname)
                        print("Success remove file:%s" % fullname)
                    except:
                        print("Can't remove file:%s" % fullname)
                if comd=='copy' and cfile[-4:] == '.pyc':
                    parent_list = parent.split("/")[:-1]
                    parent_up_path = ''
                    for i in range(len(parent_list)):
                        parent_up_path+=parent_list[i]+'/'
                    print(fullname, parent_up_path)
                    shutil.copy(fullname,parent_up_path)
                    print('update the dir of file successfully')
                if comd=='cpython' and cfile[-4:] =='.pyc':
                    cfile_name = ''
                    cfile_list = cfile.split('.')
                    for i in range(len(cfile_list)):
                        if cfile_list[i]=='cpython-36':
                            continue
                        cfile_name+=cfile_list[i]
                        if i==len(cfile_list)-1:
                            continue
                        cfile_name+='.'
                    shutil.move(fullname,os.path.join(parent,cfile_name))
                    print('update the name of the file successfully')

    else:
        print("Not an directory or Direcotry doesn't exist!")
else:
    print("Usage:")
    print("\tpython compile_pyc.py clean PATH\t\t#To clean all pyc files")
    print("\tpython compile_pyc.py compile PATH\t\t#To generate pyc files")
    print("\tpython compile_pyc.py remove PATH\t\t#To remove py files")
  1. 先將文件編譯成pyc文件:,
    python pyc_cpmpile.py compile project
    但是文件都存在各個文件夾的__pycache__中
  2. 將__pycache__中的pyc文件拷貝到原來py存在的位置:
    python pyc_compile copy project
    文件的根路徑;這樣就將編譯的文件存在原來py文件的位置了
  3. 將原來的py文件刪掉就可以了:
    python pyc_compile remove project
  4. 將文件的名字進行更改:
    python pyc_compile cpython project
    我這裏使用的是pyhon 3.6版本的,所以文件的名字變 成了*.cpython-36.pyc, 要將其中的cpython-36去掉,不然不能正常運行,因爲文件的名字默認最後一個後綴是擴展名,其他的爲文件名字
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章