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>Ok, here is the solution that I came up with based on the suggestion from @meetaig.  I'd be delighted though to accept and upvote any other ideas or suggested implementations for this situation:</p>

<pre><code>import os

def Load_from_Script_Dir(DirPath):
    os.chdir(os.path.dirname(os.path.dirname(DirPath))) ## so that the from .. import syntax later properly finds the directory.
    with open(DirPath + "__init__.py", 'w') as f:
        Scripts = [FileName for FileName in os.listdir(DirPath) if FileName[-3:] == '.py' and '__init__' not in FileName]
        for Script in Scripts: 
            f.write('from %s import *\n'  % Script.replace('.py',''))


# Script_Dir = "/path/to/Script_Dir/"  ## specify location of Script_Dir manually
Script_Dir = os.path.dirname(os.path.abspath(__file__)) + "/Script_Dir/" ## assume Script_Dir is in same path as main.py that is calling this.  Note: won't work when used in REPL.
Load_from_Script_Dir(Script_Dir)
from Script_Dir import *
</code></pre>

<p>Notes: </p>

<ol>
<li><p>this will also require that the .py files in the Script_Dir conform to naming conventions for python objects, i.e. they cannot start with numbers, cannot have spaces, etc.</p></li>
<li><p>the .py files in Script_Dir here that get loaded won't have access to objects defined in the main script (even if they are defined before calling <code>from Script_Dir import *</code>).  Likewise, they won't have access to objects defined in other scripts within Script_Dir.  Thus, it may still be necessary to use a syntax such as <code>from filename import objectname</code> within those other .py files.  This is unfortunate and partially defeats some of the convenience I was hoping to achieve, but it at least is a modest improvement over the status quo.</p></li>
</ol>

python python-2.7 module