Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is __init__.py not being called?

Tags:

python

I'm using Python 2.7 and have the following files:

./__init__.py
./aoeu.py

__init__.py has the following contents:

aoeu aoeuaoeu aoeuaoeuaoeu

so I would expect running aoeu.py to error when Python tries to load __init__.py, but it doesn't. The behavior is the same whether PYTHONPATH is set to '.' or unset.

What's going on?

like image 648
Noel Yap Avatar asked Sep 23 '11 18:09

Noel Yap


People also ask

Why is __ init __ py empty?

__init__.py can be empty, as long as it exists. It indicates that the directory should be regarded as a package.

Is __ init __ py necessary in Python?

The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.

What goes into __ init __ py?

In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

Why is my module not found Python?

Here are a few reasons why a module may not be found: you do not have the module you tried importing installed on your computer. you spelled a module incorrectly (which still links back to the previous point, that the misspelled module is not installed)...for example, spelling numpy as numpys during import.


1 Answers

__init__.py makes the enclosing directory a package. It won't be executed unless you actually try to import the package directly.

like image 103
Wooble Avatar answered Sep 26 '22 00:09

Wooble