61539

Recalling import in module

Question:

I'm still learning python and after playing around with pygame I noticed I'm re-importing things in modules I'm importing that I've already imported.

import pygame

For instance I have some classes in a separate file, but I must also import pygame into that file too for them to work. Does it actually import the code twice? Will it slow down my program? Or does it just pull the same import from cache, but if it does that, why would I need to import it again?

Is there anything like (load) in lisp that just pulls in the code like it is part of the main file?

Thank You

Answer1:

Subsequent imports pull the cached module reference from sys.modules. You need to import in order to add the module to the current namespace/scope.

Answer2:

<blockquote>

When Python imports a module, it first checks the module registry (sys.modules) to see if the module is already imported. If that’s the case, Python uses the existing module object as is.

</blockquote> <ul><li><a href="http://effbot.org/zone/import-confusion.htm" rel="nofollow">http://effbot.org/zone/import-confusion.htm</a></li> </ul>

Answer3:

<strong>Got it!</strong><br /> Okay I found what I was looking for. I just found it clunky to have to re-import code I already imported, especially when the file isn't a library or something, just split up code. found it here: <a href="http://norvig.com/python-lisp.html" rel="nofollow">http://norvig.com/python-lisp.html</a>

execfile("file.py")

Answers my problem perfectly. It parses a file and executes the code in it. Using it I was able to take out the extra import statements and it runs perfectly :D

Recommend

  • Why in Common Lisp, when I run the function “acos” with an argument of 1, is the answer is wrong?
  • Installing AllegroServe
  • multiple options for a community connector configuration in Google Data Studio
  • Identify C++ style comments with asm-mode in Emacs Lisp
  • Can I use jquery to blank textarea fields or ajax like input boxes?
  • How to lookup value with multiple criteria in excel 2007 and newer
  • Reloading table causes flickering
  • Iterate twice through a DataReader
  • C Program Compiled with Cygwin and Called from Python Hangs
  • Emacs lua-mode issue: (void-function interactively-called-p)
  • How do you create a Fuseki SPARQL server using the Apache Jena Java API?
  • Is looping through all style sheets and classes a good idea in JavaScript?
  • Count query results on multi-join statements
  • HttpClient: disabling chunked encoding
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • jQuery: add elements until a particular height is reached
  • Clear fused location provider's location for testing
  • Memory error in python- how to use more memory
  • android.support.v7.widget.Toolbar VectorDrawableCompat IllegalStateException when using support lib
  • GAE: Way to get reference to an HttpSession from its ID?
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Differences in dis-assembled C code of GCC and Borland?
  • What is the purpose of TaskExecutor in spring?
  • Loading .coffee files via a view in Rails
  • Create DicomImage from scratch using Dcmtk
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Is there a perl module to validate passwords stored in “{crypt}hashedpassword” “{ssha}hashedpassword
  • Meteor helpers not available in Angular template
  • Asynchronous UI Testing in Xcode With Swift
  • Algorithm for a smudge tool?
  • What is Eclipse's Declaration View used for?
  • Jquery - Jquery Wysiwyg return html as a string
  • SVN: Merging two branches together
  • How do I rollback to a specific git commit
  • using HTMLImports.whenReady not working in chrome
  • Append folder name and increment by 1 using batch script
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • How to load view controller without button in storyboard?