Not all .PY files are compiled to .PYC during execution

T

Tony C

After using Python for just over a year now, I've noticed something
for the first time.

I've written an application in one .PY file, and a class definition in
another.PY file. (The application instantiates one instance of the
class.)

When I run my application as in

python myapp.py

the file which contains the class definition (class.py), is compiled
to a .PYC file, but the application (myapp.py) is not.

Why is the application file not compiled to .PYC ?

thanks
 
E

Erik Max Francis

Tony said:
Why is the application file not compiled to .PYC ?

When you import a .py file, a .pyc is created. When you run it
directly, it isn't. The thinking is probably that something that's a
module is less likely to change than something that's a script. I don't
really think there's much stock to take in the difference; it's just the
way it is.
 
R

Robert M. Emmons

Tony said:
After using Python for just over a year now, I've noticed something
for the first time.

I've written an application in one .PY file, and a class definition in
another.PY file. (The application instantiates one instance of the
class.)

When I run my application as in

python myapp.py

the file which contains the class definition (class.py), is compiled
to a .PYC file, but the application (myapp.py) is not.

Why is the application file not compiled to .PYC ?

thanks
It is generally recommended that the main program be small and they you
you put most of your code in modules if you have a large program and
many modules.

I don't know officially why python works this way, but perhaps it's to
keep launching simple.

My speculation as to why it works this way is because when you create a
python script -- you generally are creating a command or program. You
don't want to have to different file names for this program --
especially if it's in the path. You want the file to have a single
launch point and this has to be source if your going to keep things simple.

Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top