0

I am reading the source code of pox controller. There is an import statement in one module called l3_editing.py which is modified based l3_learning.py.

The import statement is:

from pox.lib.recoco import Timer

Because I do not know Timer, I just along with the source tree to find this struct. But I cannot find this struct Timer, which really make me confused. It should be in pox/lib/recoco.py, but there is no module named recoco.py under lib package.

Items below pox/lib:

pox/lib

Items below pox/lib/recoco:

pox/lib/recoco

Community
  • 1
  • 1
Melvin Levett
  • 341
  • 2
  • 3
  • 11

1 Answers1

2

The lib directory is a python package. It contains an __init__.py file. When you import a package this will cause any __init__.py file to be executed. So what's in there?

Just one line:

from recoco import *

Great we are getting closer! Lets look in recoco.py:

...
class Timer (Task):
  """
  A simple timer.
...

There you have it!

Rob Bricheno
  • 4,467
  • 15
  • 29
  • 1
    thanks, I did not realize that the __init__.py will be executed. I thought that just is a tag to present that directory is a package. – Melvin Levett Nov 15 '18 at 10:23
  • sorry, I can not agree with you after I have try a simple demo. You are right on some aspects. But could you explain what have still confused me: the statement "from pox.lib.recoco import Timer", it present that I want to import Timer, and Timer is in /pox/lib/recoco, so recoco must be a module which has a suffix .py and Timer must be in recoco.py. The recoco is the end of from keyword, so it won't be a package, because I have try it by a simple demo. And this truth is understandable, because the origin which we want to import from must be a module in the end or it will not be right. – Melvin Levett Nov 15 '18 at 10:45
  • @Melvin The `recoco` in your `import` statement is a package not a module. When we import `pox.lib.recoco` we are importing the directory `pox/lib/recoco` not the file `pob/lib/recoco/recoco.py`. Because a file called `__init__.py` is present in the package directory, when we import the package then that file is executed. You might enjoy this question https://stackoverflow.com/questions/7948494/whats-the-difference-between-a-python-module-and-a-python-package – Rob Bricheno Nov 15 '18 at 11:48
  • @Melvin I will not accept your edit because the errors that you have in the example you gave me are caused by running under the Python 3 interpreter. Try your example using the Python 2.7 interpreter (which `pox` requires) and you will find it works. This behaviour changed from Python 2 to Python 3. – Rob Bricheno Nov 15 '18 at 11:50
  • thanks, I have retry it under python2.7, and it works fine. But I still think that the changes on import in python3 are more understandable. Thanks for your instruction, I have learn a lot from it, good bless to you. – Melvin Levett Nov 15 '18 at 12:01
  • @Melvin Sure, I like the python 3 way better too :-) – Rob Bricheno Nov 15 '18 at 12:03