search

Home  >  Q&A  >  body text

python - Which path does the dot in import use as the root directory?

# in mylib.py file
from . import xml
from .xml import some_thing
from ..xml import some_thing
...

Which path is used as the root directory for this type of explicit relative reference? In particular, when the above program itself is also a library, it is

by other users after it is released.
import mylib

Then what path is calculated relative to this time?

another question,

import os
assert os.path.isdir(',')

no problem. But some libraries, such as PIL

import PIL
PIL.Image(...) # 就会报错,找不到Image,需要显式的
import PIL.Image
PIL.Image(...)  # OK

This is why? Thank you in advance.

学习ing学习ing2700 days ago1027

reply all(1)I'll reply

  • 代言

    代言2017-07-05 10:36:06

    1. .(dot) is based on your current running file path as the current path

    2. After being published, it will be referenced by other users. There is a Lib directory in your Python installation directory. Directly refer to the global modules in this directory like import os

    import os
    os.path...

    It can be run because os is a module and an executable python file

    As for the PIL example, since I don’t have PIL, I can only irresponsibly guess that PIL is not a module

    reply
    0
  • Cancelreply