Skip to content

Example packages and code to better understand imports and relative imports in python3

Notifications You must be signed in to change notification settings

shailshouryya/relative-imports-in-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Overview

The alpha package mirrors the example given in https://stackoverflow.com/a/62191010 from Attempted relative import with no known parent package

Package structure:

/alpha
    __init__.py
    /beta
        __init__.py
        /delta
            __init__.py
            script.py
    /gamma
        __init__.py
        e1.py
        /epsilon
            __init__.py
            script.py
    /zeta
        __init__.py
        script.py
  • If you have a module in delta called script.py
    • alpha > beta > delta > script.py
    • and want to call the e1 module
      • alpha > gamma > e1.py
    • you can import the e1 module into the script module inside delta using
      • from ...gamma import e1
    • NOTE that if you want to run this as a script, the following will NOT work:
      • python -m alpha/beta/delta/script.py
    • If you want to run this as a script, you need to call this using:
      • python -m alpha.beta.delta.script

About

Example packages and code to better understand imports and relative imports in python3

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages