0

I have the below code in a file a1.py

fff
def test(arg):
        print 'sid'
        print arg
        print 'sid2'
test()

The above code contains 2 errors:

  1. fff does not exist and is still being asked to print

  2. Arguments are not passed in test()

Now i write an another file b1.py In that file the code is:

import a1
print 'b1 execution done'

Que 1: I executed b1.py and a1.pyc file is generated. Why ? There is a syntax error.pyc file should not have been generated?

Que2: Explain in laymen terms what is pyc file and what role does it play?

Que3: Why is pyc file generated even if a1.py has errors ?(e.g. argument not being passed)

fsociety
  • 977
  • 3
  • 12
  • 23
  • 1
    Possible duplicate of [If Python is interpreted, what are .pyc files?](http://stackoverflow.com/questions/2998215/if-python-is-interpreted-what-are-pyc-files) – vsminkov Aug 26 '16 at 19:08
  • .pyc files are compiled version of your source. If you delete them they get recreated, so that the next time your program runs, it can just use the .pyc file. I believe Q3 would be a runtime error, and that hasn't happened yet – joel goldstick Aug 26 '16 at 19:08
  • but i am trying to access a variable 'fff' which does not exist.This should have been a compile time error? Correct ? – fsociety Aug 27 '16 at 09:44

1 Answers1

0

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.

Manthan Sharma
  • 329
  • 1
  • 3
  • 12