1

The python3 version is Python 3.5.3 in my os.

mkdir  workspace
cd workspace
vim  print.py
print("i am learning")

Saved and exit.

python3 print.py
i am learning

As far as i knew, python source file was parsed and compiled into pyc file when to execute it.

ls
print.py

There is no pyc file in workspace directory,where is the complied print.py file then?

sudo find  /  -name  ".pyc"

The find command still can't search pyc file such as print.pyc . python3 -m compileall can create the compiled file for print.py manually,where is the compiled file for print.py created by python itself?
Does python3 delete the print.pyc after executing python3 print.py?

  • It's normally created when you *import* the file. If you want to manually generate it, see https://stackoverflow.com/q/5607283/3001761 – jonrsharpe Dec 01 '19 at 09:50
  • `python3 -m compileall `can create the compiled file for print.py manually,where is the compiled file for print.py created by python itself? –  Dec 01 '19 at 09:56
  • Does python3 delete the print.pyc after executing python3 print.py? –  Dec 01 '19 at 09:58
  • 1
    No, it just never saves it. – jonrsharpe Dec 01 '19 at 10:02

2 Answers2

1

Ok this is one big of a problem I ever had when I'm started to learn python few years back. Python is just like any other oop programming languages which does compilation before program execution. When python compiles its program, it creates the bite code which is you can see by standard library called dis.

import dis
print(dis.dis(your_program))

Sometimes (not always) python creates .pyc file for the running programs to improve the speed up the loading of import modules but not to improve the execution time. So hope you get intuition behind .pyc, furthermore .pyc only creates when your module is import by another module.

As an example, Imagine you have this print.py (Let's modify it shall we)

def return_print_statment(statement):
    print('Printed version: ', statement)

Suppose this module imported by another custom module called views.py. In views.py there is a module_view which will use the return_print_statment

from print import return_print_statment

def module_view():
    ...
    return_print_statment(output)

So in the compilation, since you have imported the print.py python will generate print.pyc file for it. In python 2.0 python will put the .pyc to right next to your program in the same folder, but in python3 instead of creating in the same folder python will create separate folder called __pycache__ in the same directory to put these byte codes.

Govinda Malavipathirana
  • 1,095
  • 2
  • 11
  • 29
0

python3 -m compileall .

To compile all .py files in your current directory.

http://effbot.org/pyfaq/how-do-i-create-a-pyc-file.htm

Robin
  • 45
  • 7