Skip to content

Instantly share code, notes, and snippets.

@craiga
Last active January 31, 2017 11:02
Show Gist options
  • Save craiga/cda39dcf42357192dbda7d6238d446db to your computer and use it in GitHub Desktop.
Save craiga/cda39dcf42357192dbda7d6238d446db to your computer and use it in GitHub Desktop.
Do Python modules load once and only once?
def main():
print("Importing tell the time module")
import tell_the_time
print("Importing tell the time module again")
import tell_the_time as tell_the_time_again
if __name__ == '__main__':
main()
from datetime import datetime
print(datetime.now())
@craiga
Copy link
Author

craiga commented Jan 31, 2017

Looks like modules are only imported once.

$ python --version
Python 2.7.12
$ python main.py 
Importing tell the time module
2017-01-31 11:01:06.000146
Importing tell the time module again
$ python3 --version
Python 3.5.2
$ python3 main.py 
Importing tell the time module
2017-01-31 11:01:16.400008
Importing tell the time module again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment