5

I agree that there are similar questions,but none serve my purpose.

  • I have a python script,without a .py extension.
  • I can neither change the file name nor add a symlink. The file name is significant.

  • I need to import the above mentioned file to another python script
  • I have tried the following

    >>> imp.load_source('test','.')
    <module 'test' from '.'>
    

    and

    >>> importlib.import_module('test','.')
    <module 'test' from '.'>
    

    Where the module test is just

    print 'hello world'
    

  • My requirement is that the import statement works just like it imports if the file was test.py,that is,prints hello world upon import.
  • Is there any way to "run" the module imported by using imp or imortlib?

    I would like to add that I am talking about a control file in the autotest project,if it matters.

    rjv
    • 5,499
    • 4
    • 28
    • 48

    1 Answers1

    5

    You can use imp.load_source

    >>> import imp
    >>> mod = imp.load_source("test", "test")
    hello world
    >>> mod.a
    1
    

    abc:

    print "hello world"
    a = 1 
    
    Lennart Regebro
    • 158,668
    • 41
    • 218
    • 248
    Ashwini Chaudhary
    • 232,417
    • 55
    • 437
    • 487