GithubHelp home page GithubHelp logo

python_imports_lesson's Introduction

Python 3 Imports for 6 Year Olds

Learn once and for all how to use relative and absolute imports in Python 3

Quick Notes

  • When a file is run directly, it is denoted as the __main__ file.
  • Absolute imports specify a path relative to the __main__ file.
  • Relative imports specify a path relative to the current file of the import statement.
  • The __main__ file can only include absolute imports and not relative imports.
  • Prefer absolute imports when possible, as scripts that use relative imports cannot be run directly.
  • Python 3.3 and above only allows explicit relative imports, and not implicit relative imports, so those will not be covered.

Assume this file structure.

project
├── main.py
└── my_package
    ├── my_module.py
    └── my_other_module.py

__main__ File

  • When a file is run directly it is denoted as the "__main__" file. That is, __name__ is set to __main__ for that file.
  • If a file is run indirectly, by being imported from another file, __name__ is set to the name of the module, For example, __my_module__.

Absolute Import - Case #1

in main.py

from my_package.my_module import my_var

print(my_var)

in my_package/my_module.py

my_var = 'a variable'

Now run main.py

python3 main.py

In Case #1, we include the absolute path to my_module.py and import my_var


Absolute Import - Case #2

in main.py

from my_package.my_module import my_var

print(my_var)

in my_package/my_module.py

from my_package.my_other_module import my_other_var

my_var = 'some text and ' + my_other_var

in my_package/my_other_module.py

my_other_var = 'my other variable'

Now run main.py

python3 main.py

In Case #2,

  • we include an absolute path to my_module within main.py, as well as an absolute path my_other_module in my_module.py.
  • The absolute import specifies the path relative to the __main__ file, the file being run directly from the command line, in this case, main.py.
  • If we were to try and run my_package/my_module.py directly, the import statement would break. The absolute path is assuming main.py is the __main__ file.

Relative Import - Case #1

A relative import specifies a path relative to the current file of the import statement. Let us now assume this code, changing the import statement in my_package/my_module.py

in main.py

from my_package.my_module import my_var

print(my_var)

in my_package/my_module.py

from .my_other_module import my_other_var

my_var = 'some text and ' + my_other_var

in my_package/my_other_module.py

my_other_var = 'my other variable'

Now run main.py

python3 main.py

In Case #1,

  • The import statement in my_package/my_module.py has been changed to a relative import.
  • The import statement specifies a file path relative to the current location of the import statement.
  • This code should still function properly when running python3 main.py.
  • If we were to try to run my_package/my_module.py directly, the code would break. my_module.py would then be the __main__ file and relative imports are not allowed in the __main__ file.

Relative Imports - Other Uses

Stepping into a nested directories

from .dir1.dir2.my_module import my_var

Steping out of a directory - Here we step out of our current directory to access a file at a higher level

from ..main import other_var

Stepping out of 2 Directories

from ...main import other_var
  • Using relative paths we can not run these files directly by calling python3 name_of_file.py.
  • We can however run the file as a module using the -m flag.
    • as in python3 -m name_of_file
    • notice we do not include the file extension here.

python_imports_lesson's People

Contributors

msolorio avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.