Load all modules (files) in directory without prefixes
<p>I am using python 2.7. I have a folder with a number of .py files in it that define functions, load objects, etc. that I would like to use in my main script.  I want to accomplish the following two objectives:</p>

<ol>
<li>Load all objects from all .py files in this directory, without knowing the file names in advance.</li>
<li>Access these objects in my main script without prefixing, e.g. without needing to use <code>filename.function_name()</code></li>
</ol>

<p>I understand that this does not conform to accepted best practices for python modules.  Nevertheless:</p>

<ul>
<li><p>I am writing code purely for my own use.  It will never be shared with or used by others.</p></li>
<li><p>In the course of my development work, I frequently change the names of files, move object definitions from one file to another, etc.  Thus, it is cumbersome to need to go to my main script and change the names of the function name prefixes each time I do this.</p></li>
<li><p>I am an adult.  I understand the concept of risks from name conflicts.  I employ my own naming conventions with functions and objects I create to ensure they won't conflict with other names.</p></li>
</ul>

<p>My first shot at this was to just loop through the files in the directory using <code>os.listdir()</code> and then call <code>execfile()</code> on those.  When this did not work, I reviewed the responses here: <a href="http://stackoverflow.com/questions/1057431/loading-all-modules-in-a-folder-in-python">Loading all modules in a folder in Python</a>  .  I found many helpful things, but none get me quite where I want.  Specifically, if include in my <code>__init__.py</code> file the response <a href="http://stackoverflow.com/a/1057534/3541976">here</a>:</p>

<pre><code>from os.path import dirname, basename, isfile
import glob
modules = glob.glob(dirname(__file__)+"/*.py")
__all__ = [ basename(f)[:-3] for f in modules if isfile(f)]
</code></pre>

<p>and then use in my main script:</p>

<pre><code>os.chdir("/path/to/dir/") # folder that contains `Module_Dir`
from Module_Dir import *
</code></pre>

<p>then I can gain access to all of the objects defined in the files in my directory without needing to know the names of those files ahead of time (thus satisfying #1 from my objectives).  But, this still requires me to call upon those functions and objects using <code>filename.function_name()</code>, etc.  Likewise, if I include in my <code>__init__.py</code> file explicitly:</p>

<pre><code>from filename1 import *
from filename2 import *
etc.
</code></pre>

<p>Then I can use in my main script the same <code>from Module_Dir import *</code> as above.  Now, I get access to my objects without the prefixes, but it requires me to specify explicitly the names of the files in <code>__init__.py</code>.</p>

<p>Is there a solution that can combine these two, thus accomplishing both of my objectives?  I also tried (as suggested <a href="http://stackoverflow.com/a/1057765/3541976">here</a>, for instance, including the following in <code>__init__.py</code>:</p>

<pre><code>import os
for module in os.listdir(os.path.dirname(__file__)):
    if module == '__init__.py' or module[-3:] != '.py':
        continue
    __import__(module[:-3], locals(), globals())
del module
</code></pre>

<p>But again, this still required the name prefixing.  I tried to see if there were optional arguments or modifications to how I use <code>__import__</code> here, or applications using python 2.7's importlib, but did not make progress on either front.</p>

<p>Here is another solution, based on the same general principle as in my other implementation (which is inspired by the suggestion from @meetaig in comments).  This one bypasses attempts to use the python module framework, however, and instead develops a workaround to be able to use <code>execfile()</code>.  It seems much preferable for my purposes because:</p>

<ol>
<li><p>If one support script defines functions that use functions or objects defined in other scripts, there is no need to add an extra set of <code>from filename import ...</code> to it (which as I noted in my other answer, defeated a lot of the convenience I had hoped to gain).  Instead, just as would happen if the scripts were all together in a single big file, the functions / objects only need be defined by the time a given function is called.  </p></li>
<li><p>Likewise, the support scripts can make use of objects in the main script that are created prior to the support scripts being run.</p></li>
<li><p>This avoids creating a bunch of .pyc files that just go and clutter up my directory.</p></li>
<li><p>This similarly bypasses the need (from my other solution) of making sure to navigate the interpreter's working directory to the proper location in order to ensure success of the <code>from ... import ...</code> statement in the main script.</p></li>
</ol>

<p>.</p>

<pre><code>import os

Script_List_Name = "Script_List.py"

def Load_from_Script_Dir(DirPath):
    with open(DirPath + Script_List_Name, 'w') as f:
        Scripts = [FileName for FileName in os.listdir(DirPath) if FileName[-3:] == '.py' and FileName != Script_List_Name]
        for Script in Scripts: 
            f.write('execfile("%s")\n'  % (DirPath + Script))

Script_Dir = "/path/to/Script_Dir/"

Load_from_Script_Dir(Script_Dir)
execfile(Script_Dir + Script_List_Name)
</code></pre>

<p><strong>Alternatively</strong>, if it isn't necessary to do the loading inside a function, the following loop also works just fine if included in the main script (I was thrown off initially, because when I tried to build a function to perform this loop, I think it only loaded the objects into the scope of that function, and thus they weren't accessible elsewhere, an operation which is different from other languages like Julia, where the same function would successfully put the objects into a scope outside the function):</p>

<pre><code>import os
Script_Dir = "/path/to/Script_Dir/"
for FileName in os.listdir(Script_Dir):
    if FileName[-3:] == '.py':
        print "Loading %s" %FileName
        execfile(Script_Dir + FileName)
</code></pre>

python python-2.7 module