title: Importing dynamically all modules from a folder
body: <p>I know there is <a href="https://stackoverflow.com/questions/1057431/loading-all-modules-in-a-folder-in-python">this question</a>, but not only they're not working, it's not exactly what I want. I'm developing a racing game and want to load all tracks from a folder dynamically (They're stored as .py instead of .json). I don't want to know the names of the tracks, since users can mod/add them at will. I just want to import their data. So, for example:</p>
  
  <pre><code>&gt;tracks 
  &gt;&gt;track0.py
  &gt;&gt;track1.py
  &gt;&gt;track2.py
  &gt;&gt;track3.py
  &gt;&gt;track4.py
  </code></pre>
  
  <p>Inside each track, I have data like this:</p>
  
  <pre><code>track_ground_data = [
      [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
      [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
      [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1],
      [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1],
      [1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1],
      [1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1],
      [1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1],
      [1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1],
      [1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1],
      [1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1],
      [1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1],
      [1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1],
      [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1],
      [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1],
      [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
      [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
  ]
  </code></pre>
  
  <p>I need to import each track module like this:</p>
  
  <pre><code>loaded_tracks = [t for t in tracks] # Where tracks is the folder.
  </code></pre>
  
  <p>And then access a given <code>track_ground_data</code> like this:</p>
  
  <pre><code>loaded_tracks[0].track_ground_data
  </code></pre>
  
  <p>If I knew Python was going to be so harsh with its imports, I'd have used json instead .py.</p>