What’s an __init__.py file?

Picture 19

It’s usually empty, though some times there’s some instructions in there that’s too high level for me right now.

Having an __init__.py file tells python to ‘see’ the directory it is in as a python module. Hence all packages (applications) will have an __init__.py file or several ones within each separate module .

Not having it, or having it in the wrong places will most likely throw an Unhandled Exception.


Q: If it’s empty anyway, can I just copy it into different directories?

I tried, and it doesn’t throw an error unless you copied it into a folder that isn’t meant to be a python module. Then you will get an Unhandled Exception shown in your browser.

Q: What about the pyc file that comes with it?

That woozy file __init__.pyc looks like this:


and is for computers to read. You can delete it (sometimes a .pyc file doesn’t update to reflect the changes in the py file and causes trouble), and a new .pyc file will be generated by your friend the computer when it next requires.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s