AMP⚡  Canonical

python run file in folder

You can also write relative imports, with the from module import name form of import statement.
Posted on December 2, 2019,at 10:43 PM
Kareena Trejo_Trejo     117,683 Views   
import os
import runpy


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 surroundmodule 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 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.

Trending Now