Import file by file path entered by user
<p>I am writing tester that run user scripts<br>
I.E</p>

<blockquote>
  <p>tester d:\scripts\test_scripts_list.txt</p>
</blockquote>

<p>The test_scripts_list.txt looks like this:</p>

<pre><code>test1
d:\123\test2
</code></pre>

<p>My program reads the command line with <em>argparse</em> module ,reads each line of the test_scripts_list.txt and import each test file with <strong>__<em>import</em>__</strong> (that actually run the test file)<br>
This is working well.
My only problem is to import <strong>d:\123\test2</strong> that's gives me<br>
 "ImportError: Import by filename is not supported. " </p>

<p>I think this can be solved by analyze the path name , add it to PYTHONPATH while running with<code>sys.path.append</code>  and than import it. but it seems complicated soultion.<br>
Do you have a better idea?</p>

<p>Note: test1 path is already in PYTHONPATH</p>

<p><strong>EDIT</strong>
My current solution:</p>

<pre><code># Go over each line in scripts_filename and import all tests
for line in open(scripts_file.scripts_filename):
    line = line.strip()

    if not line or  line[0] == '#':
        # Ignore empty line and line with comment
        continue
    elif line[1] == ':' and  line[2] == '\\':
        # Find the last position of '\\'
        file_name_pos =  line.rfind('\\')
        # Separate file path and filename (I.E [0:5] will take 4 first chars and [4:] will take all chars except the 4 chars)
        file_path_string =  line[0:file_name_pos+1]
        file_name_string =  line[file_name_pos+1:]
        # Append file_path_string to PYTHONPATH in order to import it later
        sys.path.append(file_path_string)

        import_name  =  file_name_string
    else:
        import_name =  line

    try:
        # Try to import test
        print  "Run test : %s" % line
        __import__(import_name)
    except ImportError:
        print "Failed! (Test not found). Continue to the next test"
</code></pre>
