Skip to Content
Modules in Python
Modules Python Files and Scope

You may remember the concept of scope from when you were learning about functions in Python. If a variable is defined inside of a function, it will not be accessible outside of the function.

Scope also applies to classes and to the files you are working within.

Files have scope? You may be wondering.

Yes. Even files inside the same directory do not have access to each other’s variables, functions, classes, or any other code. So if I have a file and another file, how do I give my hungry people access to all the sandwiches I defined?

Well, files are actually modules, so you can give a file access to another file’s content using that glorious import statement.

With a single line of from sandwiches import sandwiches at the top of, the hungry people will have all the sandwiches they could ever want.



Tab over to and define a function always_three() with no parameters that returns 3.


Call your always_three() function in Check out that error message you get in the output terminal and the consequences of file scope.


Resolve the error with an import statement at the top of that imports your function from library. Run your code and watch import do its magic!

Folder Icon

Sign up to start coding

Already have an account?