Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6974165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:12:06+00:00 2026-05-27T17:12:06+00:00

I am writing tester that run user scripts: tester d:\scripts\test_scripts_list.txt The test_scripts_list.txt looks like

  • 0

I am writing tester that run user scripts:

tester d:\scripts\test_scripts_list.txt

The test_scripts_list.txt looks like this:

test1
d:\123\test2

My program reads the command line with argparse module, reads each line of the test_scripts_list.txt and import each test file with __import__ (that actually runs the test file).
This is working well.

My only problem is to import d:\123\test2 that’s gives me:

ImportError: Import by filename is not supported.

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

Note: test1’s path is already in PYTHONPATH

EDIT
My current solution:

# 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"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:12:07+00:00Added an answer on May 27, 2026 at 5:12 pm

    See the imp module, in particular the imp.load_module and imp.load_source functions. Here’s one way you can use them (from Mercurial’s source code, slightly simplified):

    def loadpath(path):
        module_name = os.path.basename(path)[:-3]
        if os.path.isdir(path):
            # module/__init__.py style
            d, f = os.path.split(path.rstrip('/'))
            fd, fpath, desc = imp.find_module(f, [d])
            return imp.load_module(module_name, fd, fpath, desc)
        else:
            return imp.load_source(module_name, path)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application I have an application design that looks like this: web application layer
I'm writing CGI scripts in Haskell. When the user hits ‘submit’, a Haskell program
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
Writing a python program, and I came up with this error while using the
I am writing a program that repeatedly does an operation till Ctrl+C is hit
I'm writing a program that uploads huge amounts of data and I need to
I'm writing a program in java that essentially tests a bunch of things... For
I'm currently writing up some CPPunit tests for a program that tests a hardware
So I have been writing to Environment.SpecialFolder.ApplicationData this data file, that upon uninstall needs
Writing some test scripts in IronPython, I want to verify whether a window is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.