import
os
os.chdir(r
'D:\Pythonwork'
)
import
runpy
runpy.run_path(
'hello.py'
)
6.4.2. Intra-package References
When packages are structured into subpackages (as with the sound
packagein the example), you can use absolute imports to refer to submodules of siblingspackages. For example, if the module sound.filters.vocoder
needs to usethe echo
module in the sound.effects
package, it can use fromsound.effects import echo
.
You can also write relative imports, with the from module import name
formof import statement. These imports use leading dots to indicate the current andparent packages involved in the relative import. From the surround
module for example, you might use:
from . import echofrom .. import formatsfrom ..filters import equalizer
Note that relative imports are based on the name of the current module. Sincethe name of the main module is always "__main__"
, modules intended for useas the main module of a Python application must always use absolute imports.
6.4.3. Packages in Multiple Directories
Packages support one more special attribute, __path__
. This isinitialized to be a list containing the name of the directory holding thepackage’s __init__.py
before the code in that file is executed. Thisvariable can be modified; doing so affects future searches for modules andsubpackages contained in the package.
While this feature is not often needed, it can be used to extend the set ofmodules found in a package.