Search code examples
pythongeventmonkeypatching

Understanding python import of gevent


This fails for me:

import gevent
gevent.monkey.patch_all()

This works:

from gevent import monkey
monkey.patch_all()

Is there anything wrong I am trying to do by accessing gevent.monkey

Also, I am confused on this snippet:

import gevent as ge
from gevent import monkey
ge.monkey.patch_all()

what makes ge.monkey accessible now? if I comment from gevent import monkey, this doesn't work. I have used

import datetime
datetime.datetime.now()

which works perfectly fine but monkey doesn't.


Solution

  • monkey is a module inside the gevent module.

    datetime is a class inside the datetime module.

    When you import x, all of x's objects are imported (in the x namespace). But child modules are not imported

    While your top and bottom examples look identical, the type of object makes all the difference.